chore: Refactor CategoryController to improve code structure and error handling
This commit is contained in:
		@@ -3,12 +3,15 @@ import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseStatus;
 | 
			
		||||
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.CategoryService;
 | 
			
		||||
 | 
			
		||||
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.PathVariable;
 | 
			
		||||
import org.springframework.web.bind.annotation.PostMapping;
 | 
			
		||||
@@ -29,14 +32,17 @@ public class CategoryController {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/id/{id}")
 | 
			
		||||
    public CategoryDto getMethodName(@PathVariable("id") UUID id) {
 | 
			
		||||
        return categoryService.getCategoryById(id);
 | 
			
		||||
    public ResponseEntity<?> fetchCategoryById(@PathVariable("id") UUID 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
 | 
			
		||||
    public String postMethodName(@RequestBody CategoryDto dto) {
 | 
			
		||||
    public void createCategory(@RequestBody CategoryDto dto) {
 | 
			
		||||
        categoryService.addCategory(dto);        
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class CategoryService {
 | 
			
		||||
    private CategoryRepository categoryRepository;
 | 
			
		||||
@@ -25,8 +24,11 @@ public class CategoryService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user