Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/3] 꿈 일기 생성 #14

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/com/umc/dream/controller/DreamController.java
Original file line number Diff line number Diff line change
@@ -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<AddDreamResponseDto> createDream(@RequestBody DreamRequestDto dreamRequestDto) {
Dream dream = dreamService.createDream(dreamRequestDto);
AddDreamResponseDto addDreamResponseDto = DreamConverter.toAddDreamResponse(dream);
return ApiResponse.onSuccess(addDreamResponseDto);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/umc/dream/converter/DreamConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/umc/dream/converter/FeelingConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/umc/dream/converter/HashtagConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/umc/dream/converter/PeopleConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/umc/dream/converter/PlaceConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/umc/dream/domain/People.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/umc/dream/domain/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/umc/dream/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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)
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/umc/dream/dto/AddDreamResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions src/main/java/com/umc/dream/dto/DreamRequestDto.java
Original file line number Diff line number Diff line change
@@ -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<String> people;
private List<String> place;
private List<String> feeling;
private List<String> hashtag;
}
9 changes: 9 additions & 0 deletions src/main/java/com/umc/dream/repository/DreamRepository.java
Original file line number Diff line number Diff line change
@@ -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<Dream, Long> {

}
8 changes: 8 additions & 0 deletions src/main/java/com/umc/dream/repository/FeelingRepository.java
Original file line number Diff line number Diff line change
@@ -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<Feeling, Long> {
}
8 changes: 8 additions & 0 deletions src/main/java/com/umc/dream/repository/HashtagRepository.java
Original file line number Diff line number Diff line change
@@ -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<Hashtag, Long> {
}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/repository/PeopleRepository.java
Original file line number Diff line number Diff line change
@@ -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<People, Long> {
}
8 changes: 8 additions & 0 deletions src/main/java/com/umc/dream/repository/PlaceRepository.java
Original file line number Diff line number Diff line change
@@ -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<Place, Long> {
}
64 changes: 64 additions & 0 deletions src/main/java/com/umc/dream/service/DreamService.java
Original file line number Diff line number Diff line change
@@ -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<People> peopleList = new ArrayList<>();
for (String s : dreamRequestDto.getPeople()) {
People people = PeopleConverter.toPeople(s, dream);
peopleList.add(people);
}
peopleRepository.saveAll(peopleList);

List<Hashtag> hashtagList = new ArrayList<>();
for (String s : dreamRequestDto.getHashtag()) {
Hashtag hashtag = HashtagConverter.toHashtag(s, dream);
hashtagList.add(hashtag);
}
hashtagRepository.saveAll(hashtagList);

List<Feeling> feelings = new ArrayList<>();
for (String s : dreamRequestDto.getFeeling()) {
Feeling feeling = FeelingConverter.toFeeling(s, dream);
feelings.add(feeling);
}
feelingRepository.saveAll(feelings);

List<Place> placeList = new ArrayList<>();
for (String s : dreamRequestDto.getPlace()) {
Place place = PlaceConverter.toPlace(s, dream);
placeList.add(place);
}
placeRepository.saveAll(placeList);

return dream;
}
}
Loading