Using FullCalendar with Spring Boot, how do I persist an event?

Viewed 379

I'm trying to use fullCalendar in a Java Spring Boot web application. I cant seem to figure out how to get the request working that makes my event persist in my DB.

Here's my handler mapping for my controller:

@PostMapping("/event")
public void addEvent(@RequestBody Event event) {
    eventRepository.save(event);
}

My fullCalendar script:

<script>
    document.addEventListener('DOMContentLoaded', function() {
      const calendarEl = document.getElementById('calendar');
      const calendar = new FullCalendar.Calendar(calendarEl, {
        editable: true,
        selectable: true,
        dateClick: function(info) {
          if(info.view.type==="dayGridMonth"){
            this.changeView("timeGridDay",info.dateStr);
          }
          else if(info.view.type === 'timeGridDay'){
            let newEvent = calendar.addEvent({
              title : 'test1',
              start: info.date,
              end: info.date,
              description : "test4"
            });
            $.post({
              url: "/event",
              type: "POST",
              contentType: "application/json",
              data: JSON.parse(JSON.stringify(newEvent)),
            });
            console.log(info.date);
        }
        },

        headerToolbar: {
          left: 'prev, next, today',
          center: 'title',
          right: 'dayGridMonth,timeGridWeek,timeGridDay'
        },
        initialView: 'dayGridMonth',
        events : {
          url : '/allevents',
          type: 'GET',
          error: function () {
            alert('there was an error while fetching!');
          }
        }
      });

      calendar.render();
    });

  </script>

My event entity:

@Entity
@Table(name="event")
public class Event {

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "event_generator")
    @SequenceGenerator(name = "event_generator", sequenceName = "event_seq", allocationSize = 1)
    @Id
    private Long id;
    private String title;
    private String description;
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm")
    private LocalDateTime start;
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm")
    private LocalDateTime end;



    public Event() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Long getId() {
        return id;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getStart() {
        return start;
    }

    public void setStart(LocalDateTime start) {
        this.start = start;
    }

    public LocalDateTime getEnd() {
        return end;
    }

    public void setEnd(LocalDateTime end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "Event [id=" + id + ", title=" + title + ", description=" + description + ", start=" + start
                + ", end=" + end + "]";
    }
}

I either get 403 or 400 as a return.

0 Answers
Related