diff --git a/src/main/java/com/umc/dream/controller/DreamController.java b/src/main/java/com/umc/dream/controller/DreamController.java new file mode 100644 index 0000000..b7b4888 --- /dev/null +++ b/src/main/java/com/umc/dream/controller/DreamController.java @@ -0,0 +1,30 @@ +package com.umc.dream.controller; + +import com.umc.dream.apiPayload.ApiResponse; +import com.umc.dream.converter.DreamConverter; +import com.umc.dream.converter.FollowConverter; +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Follow; +import com.umc.dream.dto.AddDreamResponseDto; +import com.umc.dream.dto.DreamRequestDto; +import com.umc.dream.dto.FollowRequestDto; +import com.umc.dream.dto.FollowResponseDto; +import com.umc.dream.service.DreamService; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@AllArgsConstructor +public class DreamController { + + private DreamService dreamService; + + @PostMapping("/diary") + public ApiResponse createDream(@RequestBody DreamRequestDto dreamRequestDto) { + Dream dream = dreamService.createDream(dreamRequestDto); + AddDreamResponseDto addDreamResponseDto = DreamConverter.toAddDreamResponse(dream); + return ApiResponse.onSuccess(addDreamResponseDto); + } +} diff --git a/src/main/java/com/umc/dream/converter/DreamConverter.java b/src/main/java/com/umc/dream/converter/DreamConverter.java new file mode 100644 index 0000000..e547c89 --- /dev/null +++ b/src/main/java/com/umc/dream/converter/DreamConverter.java @@ -0,0 +1,28 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Follow; +import com.umc.dream.domain.User; +import com.umc.dream.dto.AddDreamResponseDto; +import com.umc.dream.dto.DreamRequestDto; +import com.umc.dream.dto.FollowResponseDto; + +public class DreamConverter { + public static AddDreamResponseDto toAddDreamResponse(Dream dream) { + return AddDreamResponseDto.builder() + .dream_id(dream.getId()) + .build(); + } + + public static Dream toDream(DreamRequestDto dreamRequestDto, User user) { + return Dream.builder() + .user(user) + .date(dreamRequestDto.getDate()) + .sleepTime(dreamRequestDto.getSleepTime()) + .wakeUpTime(dreamRequestDto.getWakeUpTime()) + .title(dreamRequestDto.getTitle()) + .content(dreamRequestDto.getContent()) + .category(dreamRequestDto.getCategory()) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/converter/FeelingConverter.java b/src/main/java/com/umc/dream/converter/FeelingConverter.java new file mode 100644 index 0000000..eeb7be0 --- /dev/null +++ b/src/main/java/com/umc/dream/converter/FeelingConverter.java @@ -0,0 +1,14 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Feeling; +import com.umc.dream.domain.Hashtag; + +public class FeelingConverter { + public static Feeling toFeeling(String feel, Dream dream) { + return Feeling.builder() + .feel(feel) + .dream(dream) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/converter/HashtagConverter.java b/src/main/java/com/umc/dream/converter/HashtagConverter.java new file mode 100644 index 0000000..b286086 --- /dev/null +++ b/src/main/java/com/umc/dream/converter/HashtagConverter.java @@ -0,0 +1,14 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Hashtag; +import com.umc.dream.domain.People; + +public class HashtagConverter { + public static Hashtag toHashtag(String tag, Dream dream) { + return Hashtag.builder() + .tag(tag) + .dream(dream) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/converter/PeopleConverter.java b/src/main/java/com/umc/dream/converter/PeopleConverter.java new file mode 100644 index 0000000..8aa3c7c --- /dev/null +++ b/src/main/java/com/umc/dream/converter/PeopleConverter.java @@ -0,0 +1,15 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.People; +import com.umc.dream.domain.User; +import com.umc.dream.dto.DreamRequestDto; + +public class PeopleConverter { + public static People toPeople(String name, Dream dream) { + return People.builder() + .name(name) + .dream(dream) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/converter/PlaceConverter.java b/src/main/java/com/umc/dream/converter/PlaceConverter.java new file mode 100644 index 0000000..37af48e --- /dev/null +++ b/src/main/java/com/umc/dream/converter/PlaceConverter.java @@ -0,0 +1,14 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Feeling; +import com.umc.dream.domain.Place; + +public class PlaceConverter { + public static Place toPlace(String location, Dream dream) { + return Place.builder() + .location(location) + .dream(dream) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/domain/People.java b/src/main/java/com/umc/dream/domain/People.java index cf498f9..3f3c998 100644 --- a/src/main/java/com/umc/dream/domain/People.java +++ b/src/main/java/com/umc/dream/domain/People.java @@ -2,12 +2,12 @@ import com.umc.dream.domain.common.BaseEntity; import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Entity @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class People extends BaseEntity { diff --git a/src/main/java/com/umc/dream/domain/Place.java b/src/main/java/com/umc/dream/domain/Place.java index 4939133..5d88625 100644 --- a/src/main/java/com/umc/dream/domain/Place.java +++ b/src/main/java/com/umc/dream/domain/Place.java @@ -2,12 +2,12 @@ import com.umc.dream.domain.common.BaseEntity; import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Entity @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Place extends BaseEntity { diff --git a/src/main/java/com/umc/dream/domain/User.java b/src/main/java/com/umc/dream/domain/User.java index ec379da..513611f 100644 --- a/src/main/java/com/umc/dream/domain/User.java +++ b/src/main/java/com/umc/dream/domain/User.java @@ -14,7 +14,8 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class User extends BaseEntity { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, length = 10) diff --git a/src/main/java/com/umc/dream/dto/AddDreamResponseDto.java b/src/main/java/com/umc/dream/dto/AddDreamResponseDto.java new file mode 100644 index 0000000..37365bd --- /dev/null +++ b/src/main/java/com/umc/dream/dto/AddDreamResponseDto.java @@ -0,0 +1,14 @@ +package com.umc.dream.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class AddDreamResponseDto { + private Long dream_id; +} diff --git a/src/main/java/com/umc/dream/dto/DreamRequestDto.java b/src/main/java/com/umc/dream/dto/DreamRequestDto.java new file mode 100644 index 0000000..86a0924 --- /dev/null +++ b/src/main/java/com/umc/dream/dto/DreamRequestDto.java @@ -0,0 +1,31 @@ +package com.umc.dream.dto; + +import com.umc.dream.domain.Feeling; +import com.umc.dream.domain.Hashtag; +import com.umc.dream.domain.People; +import com.umc.dream.domain.Place; +import com.umc.dream.domain.enums.DreamCategory; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.sql.Time; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Date; +import java.util.List; + +@Getter +@NoArgsConstructor +public class DreamRequestDto { + private Long users_id; + private LocalDate date; + private LocalDateTime sleepTime; + private LocalDateTime wakeUpTime; + private String title; + private String content; + private DreamCategory category; + private List people; + private List place; + private List feeling; + private List hashtag; +} diff --git a/src/main/java/com/umc/dream/repository/DreamRepository.java b/src/main/java/com/umc/dream/repository/DreamRepository.java new file mode 100644 index 0000000..90cbd97 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/DreamRepository.java @@ -0,0 +1,9 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DreamRepository extends JpaRepository { + +} diff --git a/src/main/java/com/umc/dream/repository/FeelingRepository.java b/src/main/java/com/umc/dream/repository/FeelingRepository.java new file mode 100644 index 0000000..bcecd8f --- /dev/null +++ b/src/main/java/com/umc/dream/repository/FeelingRepository.java @@ -0,0 +1,8 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Feeling; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FeelingRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/repository/HashtagRepository.java b/src/main/java/com/umc/dream/repository/HashtagRepository.java new file mode 100644 index 0000000..d767ab0 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/HashtagRepository.java @@ -0,0 +1,8 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Hashtag; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface HashtagRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/repository/PeopleRepository.java b/src/main/java/com/umc/dream/repository/PeopleRepository.java new file mode 100644 index 0000000..510a3a8 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/PeopleRepository.java @@ -0,0 +1,7 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.People; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PeopleRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/repository/PlaceRepository.java b/src/main/java/com/umc/dream/repository/PlaceRepository.java new file mode 100644 index 0000000..8f7cfd4 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/PlaceRepository.java @@ -0,0 +1,8 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Place; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PlaceRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/service/DreamService.java b/src/main/java/com/umc/dream/service/DreamService.java new file mode 100644 index 0000000..1fc43d8 --- /dev/null +++ b/src/main/java/com/umc/dream/service/DreamService.java @@ -0,0 +1,64 @@ +package com.umc.dream.service; + +import com.umc.dream.apiPayload.code.status.ErrorStatus; +import com.umc.dream.apiPayload.exception.GeneralException; +import com.umc.dream.converter.*; +import com.umc.dream.domain.*; +import com.umc.dream.dto.DreamRequestDto; +import com.umc.dream.dto.FollowRequestDto; +import com.umc.dream.repository.*; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class DreamService { + private final DreamRepository dreamRepository; + private final UserRepository userRepository; + private final PeopleRepository peopleRepository; + private final PlaceRepository placeRepository; + private final HashtagRepository hashtagRepository; + private final FeelingRepository feelingRepository; + + @Transactional + public Dream createDream(DreamRequestDto dreamRequestDto) { + User user = userRepository.findById(dreamRequestDto.getUsers_id()) + .orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); + Dream dream = DreamConverter.toDream(dreamRequestDto, user); + dreamRepository.save(dream); + + List peopleList = new ArrayList<>(); + for (String s : dreamRequestDto.getPeople()) { + People people = PeopleConverter.toPeople(s, dream); + peopleList.add(people); + } + peopleRepository.saveAll(peopleList); + + List hashtagList = new ArrayList<>(); + for (String s : dreamRequestDto.getHashtag()) { + Hashtag hashtag = HashtagConverter.toHashtag(s, dream); + hashtagList.add(hashtag); + } + hashtagRepository.saveAll(hashtagList); + + List feelings = new ArrayList<>(); + for (String s : dreamRequestDto.getFeeling()) { + Feeling feeling = FeelingConverter.toFeeling(s, dream); + feelings.add(feeling); + } + feelingRepository.saveAll(feelings); + + List placeList = new ArrayList<>(); + for (String s : dreamRequestDto.getPlace()) { + Place place = PlaceConverter.toPlace(s, dream); + placeList.add(place); + } + placeRepository.saveAll(placeList); + + return dream; + } +}