source: src/main/java/finki/it/terapijamkbackend/spring/controllers/EventController.java

Last change on this file was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 2.2 KB
Line 
1package finki.it.terapijamkbackend.spring.controllers;
2
3import finki.it.terapijamkbackend.spring.entities.Event;
4import finki.it.terapijamkbackend.spring.services.EventService;
5import jakarta.persistence.EntityNotFoundException;
6import jakarta.transaction.Transactional;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.HttpStatus;
9import org.springframework.http.ResponseEntity;
10import org.springframework.web.bind.annotation.*;
11
12import java.util.List;
13import java.util.Optional;
14
15@RestController
16@RequestMapping("/api/news")
17public 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}
Note: See TracBrowser for help on using the repository browser.