# CoffeeMaker Backend Design

This document describes the backend design of CoffeeMaker in three views: the **static design** (class diagram), the **dynamic design** (sequence diagram), and the **end point design** (REST API).

It ships describing the *starting* system. As your design changes the system, update this document — add your classes to the static design, add sequence diagrams for the scenarios you design, and document every new or changed endpoint.

See also [CoffeeMaker Architecture](architecture-design.md) for the layered architecture this design fits into, and [CoffeeMaker Requirements](requirements.md) for what the system must do.

## Static Design

![CoffeeMaker class diagram](images/coffee_maker_backend.png)

There are nine packages in the project; six of them appear in the class diagram:

- `edu.ncsu.csc326.coffee_maker.entity` — Entity classes that correspond to major objects in the system and are connected to the database. These are Data Access Objects (DAOs).
- `edu.ncsu.csc326.coffee_maker.repositories` — Repository interfaces that handle database CRUD operations. These interfaces extend `JpaRepository`.
- `edu.ncsu.csc326.coffee_maker.services` — Service interfaces that define the key business logic of the system.
- `edu.ncsu.csc326.coffee_maker.services.impl` — implementations of the Service interfaces.
- `edu.ncsu.csc326.coffee_maker.controllers` — API endpoints.
- `edu.ncsu.csc326.coffee_maker.dto` — Data Transfer Objects (DTOs) used by the Services and Controllers.

The packages not in the diagram are:

- `edu.ncsu.csc326.coffee_maker` — the `CoffeeMakerApplication` class that starts the application.
- `edu.ncsu.csc326.coffee_maker.exception` — custom exceptions for the project.
- `edu.ncsu.csc326.coffee_maker.mapper` — classes that map between Entities and DTOs.

### Entity

The `entity` package holds all persistent entities. These classes exist to store data; the functionality lives in other classes.

`Recipe` and `Inventory` are annotated with `@Entity`, which tells Spring they should be persisted to the database. Each has a corresponding database table (or tables, when composition relationships are involved).

### Repositories

The `repositories` package holds `InventoryRepository` and `RecipeRepository`. Because these interfaces extend `JpaRepository`, they provide the functionality needed to persist the entities. The generic parameters say which entity the repository works with and that the primary key is a `Long`. Additional behaviors can be added by declaring methods that follow Spring's naming conventions, like `RecipeRepository.findByName()`; Spring implements them for you.

### Services

The `services` package holds the business logic. The interfaces define the key behaviors and are implemented in the corresponding `*Impl` class in `services.impl`. Each service holds instances of the repositories it needs.

### Controllers

A REST API defines how a client communicates with the server. In CoffeeMaker, the frontend talks to the backend entirely through the REST API in the `controllers` package. Every frontend action that needs the backend has an endpoint in one of these controllers. See the classes' JavaDoc for the behavior of each.

### Adding a New Persistence Class

To add a new persistence class to CoffeeMaker, add:

- the Entity version of the persistence class
- a `*Repository` interface
- a `*Service` interface
- a `*ServiceImpl` class that implements the `*Service` interface
- a `*Controller` class with the appropriate endpoints
- a `*Dto` class that provides the Data Transfer Object version of the Entity

### Adding a Behavior

Behaviors that aren't tied to an entity — Make Recipe, for example — need:

- a `*Service` interface
- a `*ServiceImpl` class that implements the `*Service` interface
- a `*Controller` class with the appropriate endpoints

## Dynamic Design

Sequence diagrams show the flow of control through a system for one specific scenario. They are especially useful with dependency injection, where the call chain isn't obvious from reading a single class, and they show which parts of the backend a given REST call actually exercises.

For a backend sequence diagram, the starting point is a call to a REST endpoint. For `POST /makerecipe/coffee`, control transfers to `MakeRecipeController.makeRecipe()` with the amount paid as the request body. The flow of control for successfully making a recipe is:

1. Get the recipe by name.
2. Get the inventory; initialize it if it hasn't been initialized yet.
3. Make the recipe if there is enough inventory, and return the change.

![CoffeeMaker sequence diagram](images/coffee_maker_sequence.png)

## End Point Design

API documentation is the primary resource explaining what operations are possible in the system, and the reference developers return to with questions about the syntax or behavior of an endpoint.

Document each endpoint with: the name of the function, the relative URL, the HTTP verb, a brief description, the input data, the response status codes, and the response data.

### GET Routes

GET routes *fetch* data. Document the endpoint and the data returned.

#### Example

**Request:** `GET /recipes/{name}`

**Function:** `RecipeController.getRecipe(String name)`

**Description:** Returns the recipe with the given name, or 404 if no recipe with that name exists.

**Path parameters**

- `name` — the recipe's name (required)

**Response status codes**

| Status Code | Description |
| ----------- | ----------- |
| `200` | OK |
| `404` | Resource not found |

**Response**

```
Status: 200
{
  "name": "Mocha",
  "price": 10,
  "sugar": 2,
  "milk": 3,
  "coffee": 5,
  "chocolate": 3
}
```

```
Status: 404
```

### PUT/POST Routes

PUT and POST routes *send* data. Document the endpoint and the data sent to it, as well as the data returned — particularly when the response differs from the request.

#### Example

**Request:** `PUT /inventory`

**Function:** `InventoryController.updateInventory(InventoryDto inventoryDto)`

**Description:** Updates the CoffeeMaker's inventory with the values provided. The values are *added* to the amounts already stored rather than overwriting them.

Because the inventory is managed as a singleton — there is only one — the URL takes no name or id. This is unusual; PUT routes normally identify a resource in the URL.

**Body parameters**

- `inventoryDto` — an Inventory object. No id is needed here, since it is the *quantity* provided that is added to the existing values. For a resource that isn't a singleton, the id would be part of the URL.

```
{
  "sugar": 2,
  "milk": 2,
  "coffee": 3,
  "chocolate": 4
}
```

**Response status codes**

| Status Code | Description |
| ----------- | ----------- |
| `200` | OK |

**Response**

```
Status: 200
{
  "id": 1,
  "sugar": 2,
  "milk": 2,
  "coffee": 3,
  "chocolate": 4
}
```

## Your Design

Add your own design below, or in the sections above where it extends what is already there. Diagrams may be drawn in [UMLetino](https://www.umletino.com/umletino.html) and exported into `images/`, or written directly in [Mermaid](https://mermaid.js.org/), which GitHub renders inside a markdown file. Mermaid has the advantage of being text: it shows up in pull request diffs, and an AI assistant can read it without an image upload.

A Mermaid class diagram looks like this:

````
```mermaid
classDiagram
    class Ingredient {
        -Long id
        -String name
        +getName() String
    }
    class Recipe {
        -Long id
        -String name
        -int price
    }
    Recipe "1" *-- "many" Ingredient
```
````
