1 | package finki.it.terapijamkbackend.spring.controllers;
|
---|
2 |
|
---|
3 | import finki.it.terapijamkbackend.spring.entities.Event;
|
---|
4 | import finki.it.terapijamkbackend.spring.services.EventService;
|
---|
5 | import jakarta.persistence.EntityNotFoundException;
|
---|
6 | import jakarta.transaction.Transactional;
|
---|
7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
8 | import org.springframework.http.HttpStatus;
|
---|
9 | import org.springframework.http.ResponseEntity;
|
---|
10 | import org.springframework.web.bind.annotation.*;
|
---|
11 |
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Optional;
|
---|
14 |
|
---|
15 | @RestController
|
---|
16 | @RequestMapping("/api/news")
|
---|
17 | public class EventController {
|
---|
18 | @Autowired
|
---|
19 | private EventService eventService;
|
---|
20 |
|
---|
21 | @PutMapping("/createEvent")
|
---|
22 | public ResponseEntity<Event> updateEvent(@RequestBody Event eventItem) {
|
---|
23 | if (eventItem.getTitle() == null || eventItem.getText() == null) {
|
---|
24 | return ResponseEntity.badRequest().build();
|
---|
25 | }
|
---|
26 | Event createdEvent = eventService.saveEvent(eventItem);
|
---|
27 | return ResponseEntity.ok(createdEvent);
|
---|
28 | }
|
---|
29 | @GetMapping("/getAllEvents")
|
---|
30 | public ResponseEntity<List<Event>>getAllEvents(){
|
---|
31 | List<Event> eventList=eventService.getAllEvents();
|
---|
32 | return ResponseEntity.ok(eventList);
|
---|
33 | }
|
---|
34 | @DeleteMapping("/deleteEvent")
|
---|
35 | public ResponseEntity<?>deleteEntity(@RequestParam String userId){
|
---|
36 | try{
|
---|
37 | eventService.deleteById(userId);
|
---|
38 | return ResponseEntity.ok().build();
|
---|
39 | }
|
---|
40 | catch(EntityNotFoundException e){
|
---|
41 | return ResponseEntity.notFound().build();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | @PostMapping("/editEvent")
|
---|
46 | @Transactional
|
---|
47 | public ResponseEntity<Void> editEvent(@RequestParam String identifier, @RequestBody Event newEventData) {
|
---|
48 | Optional<Event> eventOptional = eventService.findByIdentifier(identifier);
|
---|
49 | if (eventOptional.isEmpty()) {
|
---|
50 | return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
---|
51 | }
|
---|
52 | Event event = eventOptional.get();
|
---|
53 | event.setTitle(newEventData.getTitle());
|
---|
54 | event.setText(newEventData.getText());
|
---|
55 | event.setImgSrc(newEventData.getImgSrc());
|
---|
56 | eventService.saveEvent(event);
|
---|
57 |
|
---|
58 | return ResponseEntity.ok().build();
|
---|
59 | }
|
---|
60 | }
|
---|