Skip to content

Commit

Permalink
Merge pull request #53 from dsc-sookmyung/feature/calendar-api
Browse files Browse the repository at this point in the history
[#31] fix: add index properties in event entity
  • Loading branch information
hellouz818 authored Mar 24, 2022
2 parents 6769cdd + ba8e495 commit b0dd876
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.answer.notinote.Child.dto.ChildDto;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.User.domain.entity.Timestamped;
import com.answer.notinote.User.domain.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand All @@ -18,7 +19,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class Child extends Timestamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
Expand Down Expand Up @@ -48,7 +48,7 @@ public void setUser(User user) {
user.setUchildren(this);
}

public void setEvents(Event event) {
public void setEvent(Event event) {
this.events.add(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.dto.EventResponseDto;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.google.api.client.util.DateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter @Setter
Expand All @@ -27,7 +31,8 @@ public ChildDto(Child child) {
this.color = child.getColor();

for(Event event : child.getEvents()) {
if (event.isRegistered()) this.events.add(new EventResponseDto(event));
if (event.isRegistered() && event.getDate().isEqual(LocalDate.now()))
this.events.add(new EventResponseDto(event));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Child create(ChildDto childDto, User user) {
return childRepository.save(child);
}

public Child findById(Long id) {
public Child findChildById(Long id) {
return childRepository.findById(id).orElseThrow(
() -> new CustomException(ErrorCode.NOT_FOUND)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ protected void configure(HttpSecurity http) throws Exception {
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.permitAll()
// .authenticated()
.and()
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
package com.answer.notinote.Event.controller;

import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Child.service.ChildService;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.dto.EventRegisterDto;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.dto.EventResponseDto;
import com.answer.notinote.Event.service.EventService;
import com.answer.notinote.Event.service.GoogleCalendarService;
import io.swagger.models.Response;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.Notice.service.NoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequiredArgsConstructor
public class EventController {

private final EventService eventService;
private final ChildService childService;
private final GoogleCalendarService calendarService;
private final NoticeService noticeService;

@PostMapping("/event")
public ResponseEntity<?> createEvent(@RequestParam(value = "id") Long id, @RequestBody EventRequestDto requestDto) {
Child child = childService.findById(id);
Notice notice = noticeService.findNoticeById(id);

Event event = eventService.create(requestDto, child);
Event event = eventService.create(requestDto, notice);

return ResponseEntity.ok(new EventResponseDto(event));
}

@PostMapping("/event/calendar")
public ResponseEntity<?> createEventInCalendar(@RequestParam(value = "id") Long id) throws GeneralSecurityException, IOException {
Event event = eventService.findEventById(id);
calendarService.createEvent(event);
@GetMapping("/event")
public ResponseEntity<?> getEventList() {
List<Event> eventList = eventService.findAll();
List<EventResponseDto> response = new ArrayList<>();
for (Event event : eventList)
response.add(new EventResponseDto(event));

return ResponseEntity.ok(new EventResponseDto(event));
return ResponseEntity.ok(response);
}

@PutMapping("/event/register")
public ResponseEntity<?> registerEvent(@RequestParam(value = "id") Long id, @RequestBody EventRegisterDto registerDto) throws GeneralSecurityException, IOException {
return ResponseEntity.ok(eventService.registerEvent(id, registerDto));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.answer.notinote.Event.domain;

import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Event.dto.EventRegisterDto;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.util.BooleanToYNConverter;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.User.domain.entity.Timestamped;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -22,37 +24,53 @@ public class Event extends Timestamped {
@Column
private Long eid;

@Column
private Long index_start;

@Column
private Long index_end;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cid")
private Child child;

@Column(length = 1000)
private String content;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "nid")
private Notice notice;

@Column(length = 50)
private String title;

@Column(length = 100)
private String description;

@Column
private LocalDate date;

@Convert(converter = BooleanToYNConverter.class)
@Column(length = 1)
private boolean registered;
private boolean registered = false;

@Convert(converter = BooleanToYNConverter.class)
@Column(length = 1)
private boolean highlight;
public Event(EventRequestDto requestDto) {
this.index_start = requestDto.getIndex_start();
this.index_end = requestDto.getIndex_end();
this.title = requestDto.getTitle();
this.date = requestDto.getDate();
}

public Event(EventRequestDto eventDto) {
this.content = eventDto.getContent();
this.date = eventDto.getDate();
this.registered = eventDto.getRegistered();
this.highlight = eventDto.getHighlight();
public void setNotice(Notice notice) {
this.notice = notice;
notice.setEvent(this);
}

public void setChild(Child child) {
this.child = child;
child.setEvents(this);
child.setEvent(this);
}

public void register() {
public void register(EventRegisterDto requestDto) {
this.title = requestDto.getTitle();
this.date = requestDto.getDate();
this.registered = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
import org.springframework.stereotype.Repository;

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
}
public interface EventRepository extends JpaRepository<Event, Long> { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.answer.notinote.Event.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;

@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class EventRegisterDto {
Long cid;
String title;
LocalDate date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import java.time.LocalDate;

@Getter @Setter
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class EventRequestDto {
private String content;
private LocalDate date;
private Boolean highlight;
private Boolean registered;
String title;
Long index_start;
Long index_end;
LocalDate date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
@Setter
public class EventResponseDto {
private Long eid;
private String content;
private String title;
private String description;
private LocalDate date;

public EventResponseDto(Event event) {
this.eid = event.getEid();
this.content = event.getContent();
this.title = event.getTitle();
this.description = event.getDescription();
this.date = event.getDate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.answer.notinote.Event.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class urlResponseDto {
String url;
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
package com.answer.notinote.Event.service;

import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Child.service.ChildService;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.domain.repository.EventRepository;
import com.answer.notinote.Event.dto.EventRegisterDto;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.dto.urlResponseDto;
import com.answer.notinote.Exception.CustomException;
import com.answer.notinote.Exception.ErrorCode;
import com.answer.notinote.Notice.domain.entity.Notice;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

@Service
@RequiredArgsConstructor
public class EventService {

private final EventRepository eventRepository;
private final ChildService childService;
private final GoogleCalendarService calendarService;

public Event create(EventRequestDto eventDto, Child child) {
Event event = new Event(eventDto);
event.setChild(child);
public Event create(EventRequestDto requestDto, Notice notice) {
Event event = new Event(requestDto);
event.setNotice(notice);
eventRepository.save(event);

return eventRepository.save(event);
return event;
}

public void registerEvent(Long id) {
@Transactional
public urlResponseDto registerEvent(Long id, EventRegisterDto requestDto) throws GeneralSecurityException, IOException {
Event event = findEventById(id);
event.register();
Child child = childService.findChildById(requestDto.getCid());

event.register(requestDto);
event.setChild(child);
eventRepository.save(event);

String url = calendarService.createEvent(event);
return new urlResponseDto(url);
}

public Event findEventById(Long id) {
return eventRepository.findById(id).orElseThrow(
() -> new CustomException(ErrorCode.NOT_FOUND)
);
}

public List<Event> findAll() {
return eventRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ public class GoogleCalendarService {
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

public void createEvent(com.answer.notinote.Event.domain.Event eventEntity) throws GeneralSecurityException, IOException {
public String createEvent(com.answer.notinote.Event.domain.Event eventEntity) throws GeneralSecurityException, IOException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();

Event event = new Event()
.setSummary(eventEntity.getContent())
.setDescription("");
.setSummary(eventEntity.getTitle())
.setDescription(eventEntity.getDescription());

//date Format: "2015-05-28T09:00:00-07:00"
DateTime startDateTime = new DateTime(eventEntity.getDate().toString()+"T09:00:00-07:00");
DateTime startDateTime = new DateTime(eventEntity.getDate().toString()+"T00:00:00+09:00");
EventDateTime start = new EventDateTime()
.setDateTime(startDateTime)
.setTimeZone("Asia/Seoul");
event.setStart(start);

DateTime endDateTime = new DateTime(eventEntity.getDate().toString()+"T17:00:00-07:00");
DateTime endDateTime = new DateTime(eventEntity.getDate().toString()+"T23:59:59+09:00");
EventDateTime end = new EventDateTime()
.setDateTime(endDateTime)
.setTimeZone("Asia/Seoul");
Expand All @@ -72,7 +72,7 @@ public void createEvent(com.answer.notinote.Event.domain.Event eventEntity) thro

String calendarId = "primary";
event = service.events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getHtmlLink());
return event.getHtmlLink();
}

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
Expand Down
Loading

0 comments on commit b0dd876

Please sign in to comment.