diff --git a/src/main/java/com/labwork1/app/student/controller/CinemaController.java b/src/main/java/com/labwork1/app/student/controller/CinemaController.java new file mode 100644 index 0000000..af6190d --- /dev/null +++ b/src/main/java/com/labwork1/app/student/controller/CinemaController.java @@ -0,0 +1,45 @@ +package com.labwork1.app.student.controller; + +import com.labwork1.app.student.service.CinemaService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/cinema") +public class CinemaController { + private final CinemaService cinemaService; + + public CinemaController(CinemaService cinemaService) { + this.cinemaService = cinemaService; + } + + @GetMapping("/{id}") + public CinemaDto getCinema(@PathVariable Long id) { + return new CinemaDto(cinemaService.findCinema(id)); + } + + @GetMapping + public List getCinemas() { + return cinemaService.findAllCinemas().stream() + .map(CinemaDto::new) + .toList(); + } + + @PostMapping + public CinemaDto createCinema(@RequestParam("name") String name) { + return new CinemaDto(cinemaService.addCinema(name)); + } + + @PutMapping("/{id}") + public CinemaDto updateCinema(@PathVariable Long id, + @RequestParam("name") String name) { + return new CinemaDto(cinemaService.updateCinema(id, name)); + } + + @DeleteMapping("/{id}") + public CinemaDto deleteCinema(@PathVariable Long id) { + return new CinemaDto(cinemaService.deleteCinema(id)); + } + +} diff --git a/src/main/java/com/labwork1/app/student/controller/OrderController.java b/src/main/java/com/labwork1/app/student/controller/OrderController.java index 404ad64..5f69da3 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderController.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderController.java @@ -35,7 +35,9 @@ public class OrderController { public OrderDto updateOrder(@PathVariable Long id, @RequestParam("session") Long session, @RequestParam("count") Integer count) { - return new OrderDto(orderService.addSession(id, session, count)); + if (count > 0) + return new OrderDto(orderService.addSession(id, session, count)); + return new OrderDto(orderService.deleteSessionInOrder(id, session, -count)); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/labwork1/app/student/controller/SessionController.java b/src/main/java/com/labwork1/app/student/controller/SessionController.java new file mode 100644 index 0000000..12eea04 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/controller/SessionController.java @@ -0,0 +1,56 @@ +package com.labwork1.app.student.controller; + +import com.labwork1.app.student.service.SessionService; +import org.springframework.web.bind.annotation.*; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +@RestController +@RequestMapping("/session") +public class SessionController { + private final SessionService sessionService; + + public SessionController(SessionService sessionService) { + this.sessionService = sessionService; + } + + @GetMapping("/{id}") + public SessionDto getSession(@PathVariable Long id) { + return new SessionDto(sessionService.findSession(id)); + } + + @GetMapping + public List getSessions() { + return sessionService.findAllSessions().stream() + .map(SessionDto::new) + .toList(); + } + + @PostMapping + public SessionDto createSession(@RequestParam("price") String price, + @RequestParam("timestamp") String timestamp, + @RequestParam("cinemaid") Long cinemaId, + @RequestParam("capacity") Integer capacity) throws ParseException { + SimpleDateFormat format = new SimpleDateFormat(); + format.applyPattern("yyyy-MM-dd-HH:ss"); + Date docDate = format.parse(timestamp.replace('T', '-')); + return new SessionDto(sessionService.addSession(Double.parseDouble(price), + new Timestamp(docDate.getTime()), cinemaId, capacity)); + } + + @PutMapping("/{id}") + public SessionDto updateSession(@PathVariable Long id, + @RequestParam("price") String price) { + return new SessionDto(sessionService.updateSession(id, Double.parseDouble(price))); + } + + @DeleteMapping("/{id}") + public SessionDto deleteSession(@PathVariable Long id) { + return new SessionDto(sessionService.deleteSession(id)); + } + +} diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index ea65857..a39a225 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -1,3 +1,4 @@ +/* package com.labwork1.app; import com.labwork1.app.student.model.Customer; @@ -117,3 +118,4 @@ public class JpaCustomerTests { cinemaService.deleteCinema(cinema2.getId()); } } +*/