Skip to content

Commit

Permalink
Merge pull request #51 from dsc-sookmyung/feature/calendar-api
Browse files Browse the repository at this point in the history
[#31] google calendar api & event entity
  • Loading branch information
hellouz818 authored Mar 23, 2022
2 parents 348bdae + 6db2c80 commit 6769cdd
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 16 deletions.
8 changes: 7 additions & 1 deletion spring/notinote/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ repositories {
}

dependencies {
// google calendar api
implementation 'com.google.api-client:google-api-client:1.33.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.32.1'
implementation 'com.google.apis:google-api-services-calendar:v3-rev20211026-1.32.1'

// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.springframework.stereotype.Controller;


@Controller
public class ChildController {


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.answer.notinote.Child.domain;

import com.answer.notinote.Child.dto.ChildDto;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.User.domain.entity.Timestamped;
import com.answer.notinote.User.domain.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand All @@ -9,6 +10,8 @@
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
Expand All @@ -32,9 +35,20 @@ public class Child extends Timestamped {
@Column()
Long color;

public Child (User user, ChildDto requestDto) {
this.user = user;
@OneToMany(mappedBy = "child", cascade = CascadeType.ALL)
private List<Event> events = new ArrayList<>();

public Child (ChildDto requestDto) {
this.cname = requestDto.getCname();
this.color = requestDto.getColor();
}

public void setUser(User user) {
this.user = user;
user.setUchildren(this);
}

public void setEvents(Event event) {
this.events.add(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@


import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.dto.EventResponseDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class ChildDto {
Long cid;
String cname;
Long color;
List<EventResponseDto> events = new ArrayList<>();

public ChildDto(Child child) {
this.cid = child.getCid();
this.cname = child.getCname();
this.color = child.getColor();

for(Event event : child.getEvents()) {
if (event.isRegistered()) this.events.add(new EventResponseDto(event));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.answer.notinote.Child.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class ChildrenResponseDto {
int event_num = 0;
List<ChildDto> children = new ArrayList<>();

public void addChild(ChildDto childDto) {
children.add(childDto);
event_num += childDto.getEvents().size();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.answer.notinote.Child.service;


import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Child.domain.repository.ChildRepository;
import com.answer.notinote.Child.dto.ChildDto;
import com.answer.notinote.Exception.CustomException;
import com.answer.notinote.Exception.ErrorCode;
import com.answer.notinote.User.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -10,4 +15,17 @@
public class ChildService {

private final ChildRepository childRepository;

public Child create(ChildDto childDto, User user) {
Child child = new Child(childDto);
child.setUser(user);

return childRepository.save(child);
}

public Child findById(Long id) {
return childRepository.findById(id).orElseThrow(
() -> new CustomException(ErrorCode.NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.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 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 java.io.IOException;
import java.security.GeneralSecurityException;

@RestController
@RequiredArgsConstructor
public class EventController {

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

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

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

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);

return ResponseEntity.ok(new EventResponseDto(event));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.answer.notinote.Event.domain;

import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.util.BooleanToYNConverter;
import com.answer.notinote.User.domain.entity.Timestamped;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDate;

@Entity
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Event extends Timestamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long eid;

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

@Column(length = 1000)
private String content;

@Column
private LocalDate date;

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

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

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

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

public void register() {
this.registered = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.answer.notinote.Event.domain.repository;

import com.answer.notinote.Event.domain.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 EventRequestDto {
private String content;
private LocalDate date;
private Boolean highlight;
private Boolean registered;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.answer.notinote.Event.dto;

import com.answer.notinote.Event.domain.Event;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
public class EventResponseDto {
private Long eid;
private String content;
private LocalDate date;

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

import com.answer.notinote.Child.domain.Child;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.domain.repository.EventRepository;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Exception.CustomException;
import com.answer.notinote.Exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class EventService {

private final EventRepository eventRepository;

public Event create(EventRequestDto eventDto, Child child) {
Event event = new Event(eventDto);
event.setChild(child);

return eventRepository.save(event);
}

public void registerEvent(Long id) {
Event event = findEventById(id);
event.register();
}

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

0 comments on commit 6769cdd

Please sign in to comment.