chore: Refactor CategoryController to improve code structure and error handling
This commit is contained in:
parent
0ca9579949
commit
31fc359d55
@ -3,12 +3,15 @@ import java.util.List;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryDto;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryDto;
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryService;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryService;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -29,14 +32,17 @@ public class CategoryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/id/{id}")
|
@GetMapping("/id/{id}")
|
||||||
public CategoryDto getMethodName(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> fetchCategoryById(@PathVariable("id") UUID id) {
|
||||||
return categoryService.getCategoryById(id);
|
CategoryDto category = categoryService.getCategoryById(id);
|
||||||
|
if(category == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
return new ResponseEntity<>(category, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public String postMethodName(@RequestBody CategoryDto dto) {
|
public void createCategory(@RequestBody CategoryDto dto) {
|
||||||
categoryService.addCategory(dto);
|
categoryService.addCategory(dto);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CategoryService {
|
public class CategoryService {
|
||||||
private CategoryRepository categoryRepository;
|
private CategoryRepository categoryRepository;
|
||||||
@ -25,8 +24,11 @@ public class CategoryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CategoryDto getCategoryById(UUID id) {
|
public CategoryDto getCategoryById(UUID id) {
|
||||||
return CategoryDtoMapper.toDto(categoryRepository.getCategoryById(id));
|
Category c = categoryRepository.getCategoryById(id);
|
||||||
|
if (c == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return CategoryDtoMapper.toDto(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user