/
klsfv
/
CalorieTracker
Обзор
Документация
Войти
/
klsfv
/
CalorieTracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
server/src/main/java/com/calorietracker/controller/MealController.java
82 строки
3 KB
klsfv
upload files
15 янв 2026, 03:19
15 янв 2026, 03:19
09dece5
Код
Авторство
О чём код?
package com.calorietracker.controller; import com.calorietracker.dto.CreateMealRequest; import com.calorietracker.dto.MealEntryDto; import com.calorietracker.service.MealService; import jakarta.validation.Valid; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; import java.util.List; @RestController @RequestMapping("/api/v1/meals") public class MealController { private final MealService mealService; public MealController(MealService mealService) { this.mealService = mealService; } @GetMapping public ResponseEntity<List<MealEntryDto>> getAll(Authentication authentication) { String userId = authentication.getName(); List<MealEntryDto> meals = mealService.getAll(userId); return ResponseEntity.ok(meals); } @GetMapping("/{id}") public ResponseEntity<MealEntryDto> getById(@PathVariable String id, Authentication authentication) { String userId = authentication.getName(); MealEntryDto meal = mealService.getById(id, userId); return ResponseEntity.ok(meal); } @GetMapping("/date/{date}") public ResponseEntity<List<MealEntryDto>> getByDate( @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, Authentication authentication) { String userId = authentication.getName(); List<MealEntryDto> meals = mealService.getByDate(userId, date); return ResponseEntity.ok(meals); } @GetMapping("/range") public ResponseEntity<List<MealEntryDto>> getByDateRange( @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate, Authentication authentication) { String userId = authentication.getName(); List<MealEntryDto> meals = mealService.getByDateRange(userId, startDate, endDate); return ResponseEntity.ok(meals); } @PostMapping public ResponseEntity<MealEntryDto> create(@Valid @RequestBody CreateMealRequest request, Authentication authentication) { String userId = authentication.getName(); MealEntryDto meal = mealService.create(userId, request); return ResponseEntity.status(HttpStatus.CREATED).body(meal); } @PutMapping("/{id}") public ResponseEntity<MealEntryDto> update(@PathVariable String id, @Valid @RequestBody CreateMealRequest request, Authentication authentication) { String userId = authentication.getName(); MealEntryDto meal = mealService.update(id, userId, request); return ResponseEntity.ok(meal); } @DeleteMapping("/{id}") public ResponseEntity<Void> delete(@PathVariable String id, Authentication authentication) { String userId = authentication.getName(); mealService.delete(id, userId); return ResponseEntity.noContent().build(); } }