Skip to content

Commit

Permalink
Merge pull request #10 from Mojacknong/feature_9/추가-API-구현
Browse files Browse the repository at this point in the history
Feature 9/추가 api 구현
  • Loading branch information
Ryeolee authored Apr 13, 2024
2 parents 184d610 + 914b0b6 commit 8d73054
Show file tree
Hide file tree
Showing 19 changed files with 573 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.controller;

import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.CheckTodayDiaryResponse;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectDiaryOneResponse;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectMyVeggieListResponse;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieDiaryService;
import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieGardenService;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/my-veggie/diary")
public class MyVeggieDiaryController {

private final MyVeggieDiaryService myVeggieDiaryService;

@PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponseDto<Void> settingMyVeggieDiary(
@RequestPart(value = "file", required = false) MultipartFile multipartFile,
@RequestParam("content") String content,
@RequestParam("isOpen") boolean isOpen,
@RequestParam("state") String state,
@RequestParam("myVeggieId") Long myVeggieId
) throws IOException {
return myVeggieDiaryService.settingMyVeggieDiary(
multipartFile,
content,
isOpen,
state,
myVeggieId
);
}

@GetMapping(value = "/{myVeggieId}/check")
public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(
@PathVariable("myVeggieId") Long myVeggieId
) {
return myVeggieDiaryService.checkTodayDiary(MyVeggie.builder().id(myVeggieId).build());
}

@GetMapping(value = "/{myVeggieId}/one")
public BaseResponseDto<SelectDiaryOneResponse> selectDiaryOne(
@PathVariable("myVeggieId") Long myVeggieId
) {
return myVeggieDiaryService.selectDiaryOne(MyVeggie.builder().id(myVeggieId).build());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.controller;

import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser;

import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRequest;
import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieGardenService;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/my-veggie")
public class MyVeggieGardenController {

private final MyVeggieGardenService myVeggieGardenService;

@PostMapping(value = "/")
public BaseResponseDto<Void> settingMyVeggie(
@AuthenticationPrincipal CustomUser user,
@Validated @RequestBody SettingMyVeggieRequest settingMyVeggi
){
return myVeggieGardenService.settingMyVeggie(
user.getUserId(),
settingMyVeggi
);
}

@GetMapping(value = "/simple-list")
public BaseResponseDto<Void> selectMyVeggieList(
@AuthenticationPrincipal CustomUser user
){
return myVeggieGardenService.selectMyVeggieList(user.getUserId());
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.controller;

import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.SettingMyVeggieRoutineReqeuest;
import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieRoutineService;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/my-veggie/routine")
public class MyVeggieRoutineController {

private final MyVeggieRoutineService myVeggieRoutineService;

@PostMapping(value = "")
public BaseResponseDto<Void> settingMyVeggieRoutine(
@Validated @RequestBody SettingMyVeggieRoutineReqeuest settingMyVeggieRoutineReqeuest
) {

return myVeggieRoutineService.settingMyVeggieRoutine(settingMyVeggieRoutineReqeuest);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request;


import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Date;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class SettingMyVeggieRequest {

@NotNull(message = "null 값을 가지면 안됩니다.")
private String nickname;

@NotNull(message = "null 값을 가지면 안됩니다.")
private Date birh;

@NotNull(message = "null 값을 가지면 안됩니다.")
private Long veggiInfoId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request;


import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class SettingMyVeggieRoutineReqeuest {

@NotNull(message = "null 값을 가지면 안됩니다.")
private String content;

private boolean notify;

private int period;

@NotNull(message = "null 값을 가지면 안됩니다.")
private Long myVeggieId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response;


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

@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Getter
public class CheckTodayDiaryResponse {

private boolean state;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response;

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

import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Getter
public class SelectDiaryOneResponse {

private String image;
private String content;
private String date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response;


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

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class SelectMyVeggieListDto {

public Long myVeggieId;
public String nickname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response;


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

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Getter

public class SelectMyVeggieListResponse {

List<SelectMyVeggieListDto> simpleMyVeggieList;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ public class MyVeggie extends BaseEntity {
private Date birth;

@Column(name = "veggie_info_id")
private String veggieInfoId;
private Long veggieInfoId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Builder.Default
private List<Routine> routines = new ArrayList<>();

@OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "myVeggie", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Builder.Default
private List<Diary> diaries = new ArrayList<>();

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_farm_club_id")
private UserFarmClub userFarmClub;

public static MyVeggie createMyVegetable(String nickname, Date birth, String veggieInfoId, User user){
public static MyVeggie createMyVegetable(String nickname, Date birth, Long veggieInfoId, User user){
MyVeggie newMyVeggie = MyVeggie.builder()
.nickname(nickname)
.birth(birth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ public class Routine extends BaseEntity {
@Column(name = "period")
private int period;

@Column(name = "notify")
private boolean notify;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "my_veggie_id")
private MyVeggie myVeggie;


public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie){
public static Routine createRoutine(Date date, String content, int period, MyVeggie myVeggie, boolean notify){
Routine newRoutine = Routine.builder()
.date(date)
.content(content)
.period(period)
.myVeggie(myVeggie)
.notify(notify)
.build();

myVeggie.addRoutine(newRoutine);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.exception;

import com.modernfarmer.farmusspring.global.response.ResponseCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum MyVeggieGardenErrorCode implements ResponseCode {

;

private final int code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.exception;

import com.modernfarmer.farmusspring.domain.test.exception.TestException;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice(basePackages = "com.modernfarmer.farmusspring.domain.test")
public class MyVeggieGardenExceptionHandler {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.exception;

import com.modernfarmer.farmusspring.global.response.ResponseCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum MyVeggieGardenSuccessCode implements ResponseCode {


NOT_FOUND_DIARY(3000, "해당 일기가 존재하지 않습니다.");
private final int code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.repository;


import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
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;
import java.util.Optional;

@Repository
public interface MyVeggieRepository extends JpaRepository<MyVeggie, Long> {

Optional<MyVeggie> findById(Long id);

@Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie ORDER BY d.createdDate DESC")
List<Diary> findDiariesByMyVeggie(MyVeggie myVeggie);

@Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie AND FUNCTION('DATE', d.createdDate) = CURRENT_DATE ")
Diary findDiariesByMyVeggieAndToday(MyVeggie myVeggie);

@Query("SELECT mv FROM my_veggie AS mv JOIN FETCH mv.user WHERE mv.user.id = :userId")
List<MyVeggie> findMyVeggieUserId(@Param("userId") Long userId);







}
Loading

0 comments on commit 8d73054

Please sign in to comment.