Skip to content
Snippets Groups Projects
Commit ab634178 authored by Stian Lyng Stræte's avatar Stian Lyng Stræte
Browse files

Merge branch 'feature/67-create-shoppinglist-service-and-controller' into 'main'

Feature/67 create shoppinglist service and controller

See merge request idatt2106-v23-03/backend!41
parents 9eec4cb7 1f231701
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.request.ShoppingListRequest;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.service.ShoppingListService;
/**
* Controller for the shopping list
*
* @author Stian Lyng
* @version 1.0
*/
@AllArgsConstructor
@RestController
@RequestMapping("/api/shoppinglist")
public class ShoppingListController {
@Autowired
ShoppingListService shoppingListService;
/**
* Creates a shopping list
*
* @param request the request containing the group ID
* @return the created shopping list, or an error if the group ID is invalid
*/
@PostMapping("/add")
public ResponseEntity<ShoppingList> createShoppingList(@RequestBody ShoppingListRequest request) {
return ResponseEntity.status(HttpStatus.CREATED).body(shoppingListService.createShoppingList(request));
}
/**
* Gets a shopping list by its ID
*
* @param request the request containing the shopping list ID
* @return the shopping list, or an error if the ID is invalid
*/
@GetMapping("/{id}")
public ResponseEntity<ShoppingList> getShoppingListById(@RequestBody ShoppingListRequest request) {
Optional<ShoppingList> shoppingList = shoppingListService.getShoppingListById(request.getGroupID());
return shoppingList.map(list -> ResponseEntity.status(HttpStatus.OK).body(list))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}
/**
* Gets a shopping list by its group ID
*
* @param request the request containing the group ID
* @return the shopping list, or an error if the ID is invalid
*/
@GetMapping("/group/{id}")
public ResponseEntity<ShoppingList> getAllShoppingListsByGroupId(@RequestBody ShoppingListRequest request) {
Optional<ShoppingList> shoppingList = shoppingListService.getShoppingListByGroupId(request.getGroupID());
return shoppingList.map(list -> ResponseEntity.status(HttpStatus.OK).body(list))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}
}
package ntnu.idatt2016.v233.SmartMat.dto.request;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
/**
* This class represents a request for a shopping list.
*/
@Data
@Builder
@AllArgsConstructor
public class ShoppingListRequest {
/**
* The unique identifier of the shopping list.
*/
private long id;
/**
* The unique identifier of the group for which the shopping list is requested.
*/
private long groupID;
}
......@@ -3,6 +3,10 @@ package ntnu.idatt2016.v233.SmartMat.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ntnu.idatt2016.v233.SmartMat.dto.request.ShoppingListRequest;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
......@@ -12,24 +16,20 @@ import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
* @author Stian Lyng
* @version 1.1
*/
@Service
public class ShoppingListService {
@Autowired
ShoppingListRepository shoppingListRepository;
/**
* Creates a new ShoppingListService
*
* @param shoppingListRepository The repository to use
*/
public ShoppingListService(ShoppingListRepository shoppingListRepository) {
this.shoppingListRepository = shoppingListRepository;
}
/**
* Saves a shopping list to the database
* Create and save a shopping list to the database
* @param shoppingList the shopping list to save
* @return the saved shopping list
*/
public ShoppingList saveShoppingList(ShoppingList shoppingList) {
public ShoppingList createShoppingList(ShoppingListRequest shoppingListRequest) {
ShoppingList shoppingList = new ShoppingList();
shoppingList.setGroupID(shoppingListRequest.getGroupID());
return shoppingListRepository.save(shoppingList);
}
......@@ -49,7 +49,7 @@ public class ShoppingListService {
* @param id the ID of the group
* @return an optional containing the shopping list if it exists
*/
public Optional<ShoppingList> getShoppingListByGroupId(int id) {
public Optional<ShoppingList> getShoppingListByGroupId(long id) {
return shoppingListRepository.getByGroupID(id);
}
......@@ -78,7 +78,9 @@ public class ShoppingListService {
* @param id the ID of the shopping list
*/
public void deleteShoppingListById(long id) {
shoppingListRepository.deleteById(id);
Optional<ShoppingList> shoppingList = shoppingListRepository.findById(id);
if (shoppingList.isPresent()) {
shoppingListRepository.deleteById(id);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment