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/#45 memo #46

Merged
merged 4 commits into from
Nov 14, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.umc.commonplant.domain.memo.controller;

import com.umc.commonplant.domain.Jwt.JwtService;
import com.umc.commonplant.domain.memo.dto.MemoDto;
import com.umc.commonplant.domain.memo.service.MemoService;
import com.umc.commonplant.domain.story.dto.StoryDto;
import com.umc.commonplant.domain.user.entity.User;
import com.umc.commonplant.domain.user.service.UserService;
import com.umc.commonplant.global.dto.JsonResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RequestMapping("/memo")
@RequiredArgsConstructor
@RestController
public class MemoController {

private final MemoService memoService;
private final JwtService jwtService;
private final UserService userService;

@PostMapping("/add")
public ResponseEntity<JsonResponse> createMemo(@RequestPart("memoRequest")MemoDto.MemoRequest memoRequest, @RequestPart("image") MultipartFile multipartFile){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

memoService.createMemo(user, memoRequest, multipartFile);

return ResponseEntity.ok(new JsonResponse(true, 200, "createMemo", null));
}

@PatchMapping("/update")
public ResponseEntity<JsonResponse> updateMemo(@RequestPart("memoRequest")MemoDto.MemoUpdateRequest memoUpdateRequest, @RequestPart("image") MultipartFile multipartFile) {
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

memoService.updateMemo(user, memoUpdateRequest, multipartFile);

return ResponseEntity.ok(new JsonResponse(true, 200, "updateMemo", null));
}

@DeleteMapping("/delete")
public ResponseEntity<JsonResponse> deleteMemo(@RequestParam("memo_idx") Long memo_idx) {
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

memoService.deleteMemo(user, memo_idx);

return ResponseEntity.ok(new JsonResponse(true, 200, "deleteMemo", null));
}

@GetMapping("/one-memo")
public ResponseEntity<JsonResponse> getOneMemo(@RequestParam("memo_idx") Long memo_idx) {
MemoDto.GetOneMemo memoResponse = memoService.getOneMemo(memo_idx);
return ResponseEntity.ok(new JsonResponse(true, 200, "get one-memo", memoResponse));
}

@GetMapping("/get-plantmemo")
public ResponseEntity<JsonResponse> getPlantMemo(@RequestParam("plant_idx") Long plant_idx) {
List<MemoDto.GetAllMemo> memoResponseList = memoService.getAllMemoByPlant(plant_idx);
return ResponseEntity.ok(new JsonResponse(true, 200, "get one-memo", memoResponseList));
}

}
84 changes: 84 additions & 0 deletions src/main/java/com/umc/commonplant/domain/memo/dto/MemoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.umc.commonplant.domain.memo.dto;

import com.umc.commonplant.domain.memo.entity.Memo;
import com.umc.commonplant.domain.plant.entity.Plant;
import com.umc.commonplant.domain2.info.entity.Info;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

public class MemoDto {

@NoArgsConstructor
@Data
public static class MemoRequest {
private Long plant_idx;
private String content;

@Builder
public MemoRequest(Long plant_idx, String content) {
this.plant_idx = plant_idx;
this.content = content;
}

}

@NoArgsConstructor
@Data
public static class MemoUpdateRequest {
private Long memo_idx;
private Long plant_idx;
private String content;
private String imgUrl;

@Builder
public MemoUpdateRequest(Long memo_idx, Long plant_idx, String content, String imgUrl) {
this.memo_idx = memo_idx;
this.plant_idx = plant_idx;
this.content = content;
this.imgUrl = imgUrl;
}

}

@NoArgsConstructor
@Data
public static class GetOneMemo {
private Long memo_idx;
private Long plant_idx;
private String content;
private String imgUrl;

@Builder
public GetOneMemo(Long memo_idx, Long plant_idx, String content, String imgUrl) {
this.memo_idx = memo_idx;
this.plant_idx = plant_idx;
this.content = content;
this.imgUrl = imgUrl;
}

}

@NoArgsConstructor
@Data
public static class GetAllMemo {
private Long memo_idx;
private String content;
private String imgUrl;
private String writer;
private LocalDateTime created_at;

@Builder
public GetAllMemo(Long memo_idx, String content, String imgUrl, String writer, LocalDateTime created_at) {
this.memo_idx = memo_idx;
this.content = content;
this.imgUrl = imgUrl;
this.writer = writer;
this.created_at = created_at;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.umc.commonplant.domain.BaseTime;
import com.umc.commonplant.domain.plant.entity.Plant;
import com.umc.commonplant.domain.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
Expand All @@ -11,8 +13,10 @@
import javax.persistence.*;

@Getter
@Builder
@Table(name = "memo")
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Memo extends BaseTime {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.umc.commonplant.domain.memo.entity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemoRepository extends JpaRepository<Memo, Long> {

@Query("SELECT m FROM Memo m WHERE m.plant.plantIdx = :plantIdx")
List<Memo> findByPlantIdx(@Param("plantIdx") Long plantIdx);
}
144 changes: 144 additions & 0 deletions src/main/java/com/umc/commonplant/domain/memo/service/MemoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.umc.commonplant.domain.memo.service;

import com.umc.commonplant.domain.image.service.ImageService;
import com.umc.commonplant.domain.memo.dto.MemoDto;
import com.umc.commonplant.domain.memo.entity.Memo;
import com.umc.commonplant.domain.memo.entity.MemoRepository;
import com.umc.commonplant.domain.plant.entity.Plant;
import com.umc.commonplant.domain.plant.entity.PlantRepository;
import com.umc.commonplant.domain.user.entity.User;
import com.umc.commonplant.global.exception.ErrorResponseStatus;
import com.umc.commonplant.global.exception.GlobalException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class MemoService {
private final MemoRepository memoRepository;
private final ImageService imageService;
private final PlantRepository plantRepository;


public void createMemo(User user, MemoDto.MemoRequest memoRequest, MultipartFile multipartFile) {
if (memoRequest == null) throw new GlobalException(ErrorResponseStatus.EMPTY_INPUT_MEMO);
if (memoRequest.getContent() == null) throw new GlobalException(ErrorResponseStatus.EMPTY_CONTENT_MEMO);
if (memoRequest.getContent().length() > 200) throw new GlobalException(ErrorResponseStatus.OVERFLOW_CONTENT_MEMO);

String imgUrl = null;
if(multipartFile != null && !multipartFile.isEmpty()) {
imgUrl = imageService.saveImage(multipartFile);
}

Plant plant = plantRepository.findByPlantIdx(memoRequest.getPlant_idx())
.orElseThrow(() -> new GlobalException(ErrorResponseStatus.PLANT_NOT_FOUND));

Memo memo = Memo.builder()
.user(user)
.plant(plant)
.imgUrl(imgUrl)
.content(memoRequest.getContent())
.build();

try {
memoRepository.save(memo);
} catch (Exception e) {
throw new GlobalException(ErrorResponseStatus.DATABASE_ERROR);
}
}

public void updateMemo(User user, MemoDto.MemoUpdateRequest memoUpdateRequest, MultipartFile multipartFile) {
if (memoUpdateRequest == null) throw new GlobalException(ErrorResponseStatus.EMPTY_INPUT_MEMO);
if (memoUpdateRequest.getContent() == null) throw new GlobalException(ErrorResponseStatus.EMPTY_CONTENT_MEMO);
if (memoUpdateRequest.getContent().length() > 200) throw new GlobalException(ErrorResponseStatus.OVERFLOW_CONTENT_MEMO);

Optional<Memo> existingMemoOptional = memoRepository.findById(memoUpdateRequest.getMemo_idx());
if(existingMemoOptional.isEmpty()) throw new GlobalException(ErrorResponseStatus.NOT_EXIST_MEMO);
Memo existingMemo = existingMemoOptional.get();

if(!Objects.equals(existingMemo.getUser().getUserIdx(), user.getUserIdx())) {
throw new GlobalException(ErrorResponseStatus.UNAUTHORIZED_USER_MEMO);
}

String imgUrl = null;
if(multipartFile != null && !multipartFile.isEmpty()) {
imgUrl = imageService.saveImage(multipartFile);
} else {
if(Objects.equals(existingMemo.getImgUrl(), memoUpdateRequest.getImgUrl())) {
imgUrl = memoUpdateRequest.getImgUrl();
}
}

Memo memo = Memo.builder()
.memoIdx(memoUpdateRequest.getMemo_idx())
.user(user)
.plant(existingMemo.getPlant())
.imgUrl(imgUrl)
.content(memoUpdateRequest.getContent())
.build();

try {
memoRepository.save(memo);
} catch (Exception e) {
throw new GlobalException(ErrorResponseStatus.DATABASE_ERROR);
}
}

public void deleteMemo(User user, Long memoIdx) {
Optional<Memo> existingMemoOptional = memoRepository.findById(memoIdx);
if(existingMemoOptional.isEmpty()) throw new GlobalException(ErrorResponseStatus.NOT_EXIST_MEMO);
Memo existingMemo = existingMemoOptional.get();

if(!Objects.equals(existingMemo.getUser().getUserIdx(), user.getUserIdx())) {
throw new GlobalException(ErrorResponseStatus.UNAUTHORIZED_DELETE_MEMO);
}

try {
memoRepository.deleteById(memoIdx);
} catch (Exception e) {
throw new GlobalException(ErrorResponseStatus.DATABASE_ERROR);
}
}


public MemoDto.GetOneMemo getOneMemo(Long memoIdx) {
Optional<Memo> existingMemoOptional = memoRepository.findById(memoIdx);
if(existingMemoOptional.isEmpty()) throw new GlobalException(ErrorResponseStatus.NOT_EXIST_MEMO);
Memo existingMemo = existingMemoOptional.get();

return MemoDto.GetOneMemo.builder()
.memo_idx(existingMemo.getMemoIdx())
.plant_idx(existingMemo.getPlant().getPlantIdx())
.content(existingMemo.getContent())
.imgUrl(existingMemo.getImgUrl())
.build();
}

public List<MemoDto.GetAllMemo> getAllMemoByPlant(Long plantIdx) {
List<Memo> existingMemo = memoRepository.findByPlantIdx(plantIdx);
List<Memo> sortedMemos = existingMemo.stream()
.sorted(Comparator.comparing(Memo::getCreatedAt).reversed())
.collect(Collectors.toList());

List<MemoDto.GetAllMemo> memoResponseList = new ArrayList<>();

for(Memo memo : sortedMemos) {
MemoDto.GetAllMemo getAllMemo = MemoDto.GetAllMemo.builder()
.memo_idx(memo.getMemoIdx())
.content(memo.getContent())
.imgUrl(memo.getImgUrl())
.writer(memo.getUser().getName())
.created_at(memo.getCreatedAt())
.build();

memoResponseList.add(getAllMemo);
}

return memoResponseList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public enum ErrorResponseStatus {


// 4300 : Memo
PLANT_NOT_FOUND(false, 4300, "λ“±λ‘λ˜μ§€ μ•Šμ€ μ‹λ¬Όμž…λ‹ˆλ‹€."),
NOT_EXIST_MEMO(false, 4301, "μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ©”λͺ¨μž…λ‹ˆλ‹€."),
EMPTY_CONTENT_MEMO(false, 4302, "λ©”λͺ¨μ˜ λ‚΄μš©μ„ μž…λ ₯ν•΄ μ£Όμ„Έμš”"),
OVERFLOW_CONTENT_MEMO(false, 4303, "λ©”λͺ¨μ˜ λ‚΄μš©μ€ 200자 μ΄λ‚΄λ‘œ μž…λ ₯ν•΄ μ£Όμ„Έμš”"),
EMPTY_INPUT_MEMO(false, 4304, "λ©”λͺ¨λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”"),
UNAUTHORIZED_USER_MEMO(false, 4305, "λ©”λͺ¨ μž‘μ„±μžλ§Œ μˆ˜μ •μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€"),
UNAUTHORIZED_DELETE_MEMO(false, 4306, "λ©”λͺ¨ μž‘μ„±μžλ§Œ μ‚­μ œκ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€"),

// 4400 : Information

Expand Down