package ru.ulstu.timeline.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.service.EventService;

import java.util.List;

import static ru.ulstu.timeline.controller.EventController.URL;

@RestController
@RequestMapping(URL)
public class EventController {
    public static final String URL = Constants.API_1_0 + "events";

    private final EventService eventService;

    public EventController(EventService eventService) {
        this.eventService = eventService;
    }

    @GetMapping
    public Response<List<EventDto>> getEvents() {
        return new Response<>(eventService.findAll());
    }

    @PostMapping
    public Response createEvent(@RequestBody EventDto timelineDto) {
        return new Response(eventService.create(timelineDto));
    }

    @PutMapping
    public Response updateEvent(@RequestBody EventDto eventDto) {
        return new Response(eventService.update(eventDto));
    }

    @DeleteMapping("/{event-id}")
    public Response delete(@PathVariable("event-id") Integer eventId) {
        eventService.delete(eventId);
        return new Response(true);
    }
}
