контроллеры

This commit is contained in:
dasha 2023-04-07 15:28:42 +04:00
parent 465dcc3fae
commit 9d40b79426
4 changed files with 106 additions and 1 deletions

View File

@ -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<CinemaDto> 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));
}
}

View File

@ -35,7 +35,9 @@ public class OrderController {
public OrderDto updateOrder(@PathVariable Long id, public OrderDto updateOrder(@PathVariable Long id,
@RequestParam("session") Long session, @RequestParam("session") Long session,
@RequestParam("count") Integer count) { @RequestParam("count") Integer count) {
if (count > 0)
return new OrderDto(orderService.addSession(id, session, count)); return new OrderDto(orderService.addSession(id, session, count));
return new OrderDto(orderService.deleteSessionInOrder(id, session, -count));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -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<SessionDto> 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));
}
}

View File

@ -1,3 +1,4 @@
/*
package com.labwork1.app; package com.labwork1.app;
import com.labwork1.app.student.model.Customer; import com.labwork1.app.student.model.Customer;
@ -117,3 +118,4 @@ public class JpaCustomerTests {
cinemaService.deleteCinema(cinema2.getId()); cinemaService.deleteCinema(cinema2.getId());
} }
} }
*/