add event controller
This commit is contained in:
parent
43917c043d
commit
aa919906aa
@ -1,12 +1,15 @@
|
|||||||
package ru.ulstu.core.util;
|
package ru.ulstu.core.util;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class StreamApiUtils {
|
public class StreamApiUtils {
|
||||||
|
|
||||||
public static <T, R> List<T> convert(List<R> entitites, Function<R, T> converter) {
|
public static <T, R> List<T> convert(List<R> entities, Function<R, T> converter) {
|
||||||
return entitites.stream().map(e -> converter.apply(e)).collect(Collectors.toList());
|
return entities == null
|
||||||
|
? Collections.EMPTY_LIST
|
||||||
|
: entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
68
src/main/java/ru/ulstu/timeline/model/Event.java
Normal file
68
src/main/java/ru/ulstu/timeline/model/Event.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package ru.ulstu.timeline.model;
|
||||||
|
|
||||||
|
import org.hibernate.validator.constraints.NotBlank;
|
||||||
|
import ru.ulstu.core.model.BaseEntity;
|
||||||
|
import ru.ulstu.user.model.User;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Event extends BaseEntity {
|
||||||
|
@NotBlank
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(name = "create_date")
|
||||||
|
private Date createDate;
|
||||||
|
|
||||||
|
@Column(name = "update_date")
|
||||||
|
private Date updateDate;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
private List<User> recipients;
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateDate(Date createDate) {
|
||||||
|
this.createDate = createDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getUpdateDate() {
|
||||||
|
return updateDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUpdateDate(Date updateDate) {
|
||||||
|
this.updateDate = updateDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> getRecipients() {
|
||||||
|
return recipients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecipients(List<User> recipients) {
|
||||||
|
this.recipients = recipients;
|
||||||
|
}
|
||||||
|
}
|
67
src/main/java/ru/ulstu/timeline/model/EventDto.java
Normal file
67
src/main/java/ru/ulstu/timeline/model/EventDto.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package ru.ulstu.timeline.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import ru.ulstu.user.model.UserDto;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
public class EventDto {
|
||||||
|
private final Integer id;
|
||||||
|
private final String title;
|
||||||
|
private final Date createDate;
|
||||||
|
private final Date updateDate;
|
||||||
|
private final String description;
|
||||||
|
private final List<UserDto> recipients;
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public EventDto(@JsonProperty("id") Integer id,
|
||||||
|
@JsonProperty("title") String title,
|
||||||
|
@JsonProperty("createDate") Date createDate,
|
||||||
|
@JsonProperty("updateDate") Date updateDate,
|
||||||
|
@JsonProperty("description") String description,
|
||||||
|
@JsonProperty("recipients") List<UserDto> recipients) {
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
this.createDate = createDate;
|
||||||
|
this.updateDate = updateDate;
|
||||||
|
this.description = description;
|
||||||
|
this.recipients = recipients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventDto(Event event) {
|
||||||
|
this.id = event.getId();
|
||||||
|
this.title = event.getTitle();
|
||||||
|
this.createDate = event.getCreateDate();
|
||||||
|
this.updateDate = event.getUpdateDate();
|
||||||
|
this.description = event.getDescription();
|
||||||
|
this.recipients = convert(event.getRecipients(), UserDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getUpdateDate() {
|
||||||
|
return updateDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserDto> getRecipients() {
|
||||||
|
return recipients;
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/ru/ulstu/timeline/model/Timeline.java
Normal file
25
src/main/java/ru/ulstu/timeline/model/Timeline.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package ru.ulstu.timeline.model;
|
||||||
|
|
||||||
|
import ru.ulstu.core.model.BaseEntity;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Timeline extends BaseEntity {
|
||||||
|
|
||||||
|
@OneToMany(cascade = CascadeType.ALL)
|
||||||
|
@JoinColumn(name = "timeline_id")
|
||||||
|
private List<Event> events;
|
||||||
|
|
||||||
|
public List<Event> getEvents() {
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEvents(List<Event> events) {
|
||||||
|
this.events = events;
|
||||||
|
}
|
||||||
|
}
|
33
src/main/java/ru/ulstu/timeline/model/TimelineDto.java
Normal file
33
src/main/java/ru/ulstu/timeline/model/TimelineDto.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package ru.ulstu.timeline.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
public class TimelineDto {
|
||||||
|
private final Integer id;
|
||||||
|
private final List<EventDto> events;
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public TimelineDto(@JsonProperty("id") Integer id,
|
||||||
|
@JsonProperty("events") List<EventDto> events) {
|
||||||
|
this.id = id;
|
||||||
|
this.events = events;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimelineDto(Timeline timeline) {
|
||||||
|
this.id = timeline.getId();
|
||||||
|
this.events = convert(timeline.getEvents(), EventDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<EventDto> getEvents() {
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulstu.timeline.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.timeline.model.Event;
|
||||||
|
|
||||||
|
public interface EventRepository extends JpaRepository<Event, Integer> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulstu.timeline.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.timeline.model.Timeline;
|
||||||
|
|
||||||
|
public interface TimelineRepository extends JpaRepository<Timeline, Integer> {
|
||||||
|
}
|
61
src/main/java/ru/ulstu/timeline/service/EventService.java
Normal file
61
src/main/java/ru/ulstu/timeline/service/EventService.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package ru.ulstu.timeline.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulstu.timeline.model.Event;
|
||||||
|
import ru.ulstu.timeline.model.EventDto;
|
||||||
|
import ru.ulstu.timeline.repository.EventRepository;
|
||||||
|
import ru.ulstu.user.model.User;
|
||||||
|
import ru.ulstu.user.model.UserDto;
|
||||||
|
import ru.ulstu.user.service.UserService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class EventService {
|
||||||
|
|
||||||
|
private final EventRepository eventRepository;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
public EventService(EventRepository eventRepository,
|
||||||
|
UserService userService) {
|
||||||
|
this.eventRepository = eventRepository;
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<EventDto> findAll() {
|
||||||
|
return convert(eventRepository.findAll(), EventDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public int create(EventDto eventDto) {
|
||||||
|
return eventRepository.save(copyFromDto(new Event(), eventDto)).getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Event copyFromDto(Event event, EventDto eventDto) {
|
||||||
|
event.setCreateDate(eventDto.getCreateDate());
|
||||||
|
event.setDescription(eventDto.getDescription());
|
||||||
|
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
|
||||||
|
event.setTitle(eventDto.getTitle());
|
||||||
|
event.setUpdateDate(eventDto.getUpdateDate());
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Integer update(EventDto eventDto) {
|
||||||
|
Event event = eventRepository.findOne(eventDto.getId());
|
||||||
|
return eventRepository.save(copyFromDto(event, eventDto)).getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void delete(Integer timelineId) {
|
||||||
|
Event event = eventRepository.findOne(timelineId);
|
||||||
|
eventRepository.delete(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Event> findByIds(List<Integer> ids) {
|
||||||
|
return eventRepository.findAll(ids);
|
||||||
|
}
|
||||||
|
}
|
51
src/main/java/ru/ulstu/timeline/service/TimelineService.java
Normal file
51
src/main/java/ru/ulstu/timeline/service/TimelineService.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package ru.ulstu.timeline.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulstu.timeline.model.EventDto;
|
||||||
|
import ru.ulstu.timeline.model.Timeline;
|
||||||
|
import ru.ulstu.timeline.model.TimelineDto;
|
||||||
|
import ru.ulstu.timeline.repository.TimelineRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TimelineService {
|
||||||
|
|
||||||
|
private final TimelineRepository timelineRepository;
|
||||||
|
private final EventService eventService;
|
||||||
|
|
||||||
|
public TimelineService(TimelineRepository timelineRepository,
|
||||||
|
EventService eventService) {
|
||||||
|
this.timelineRepository = timelineRepository;
|
||||||
|
this.eventService = eventService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TimelineDto> findAll() {
|
||||||
|
return convert(timelineRepository.findAll(), TimelineDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public int create(TimelineDto timelineDto) {
|
||||||
|
return timelineRepository.save(copyFromDto(new Timeline(), timelineDto)).getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Timeline copyFromDto(Timeline timeline, TimelineDto timelineDto) {
|
||||||
|
timeline.setEvents(eventService.findByIds(convert(timelineDto.getEvents(), EventDto::getId)));
|
||||||
|
return timeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Integer update(TimelineDto timelineDto) {
|
||||||
|
Timeline timeline = timelineRepository.findOne(timelineDto.getId());
|
||||||
|
return timelineRepository.save(copyFromDto(timeline, timelineDto)).getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void delete(Integer timelineId) {
|
||||||
|
Timeline timeline = timelineRepository.findOne(timelineId);
|
||||||
|
timelineRepository.delete(timeline);
|
||||||
|
}
|
||||||
|
}
|
@ -282,4 +282,8 @@ public class UserService implements UserDetailsService {
|
|||||||
.map(role -> new SimpleGrantedAuthority(role.getName()))
|
.map(role -> new SimpleGrantedAuthority(role.getName()))
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<User> findByIds(List<Integer> ids) {
|
||||||
|
return userRepository.findAll(ids);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user