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

#14/record #21

Merged
merged 52 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
12bc4b8
application.yml 추가
SeWooooong Mar 12, 2023
e7f560e
Create deploy.yml
SeWooooong Mar 13, 2023
c33262f
자동 배포 설정
SeWooooong Mar 13, 2023
dbf13fa
Merge remote-tracking branch 'origin/main'
SeWooooong Mar 13, 2023
1efd309
자동 배포 설정
SeWooooong Mar 13, 2023
2e9f09e
자동 배포 설정
SeWooooong Mar 13, 2023
078a500
자동 배포 설정
SeWooooong Mar 13, 2023
55aa109
자동 배포 설정
SeWooooong Mar 13, 2023
5f5a278
자동 배포 설정
SeWooooong Mar 13, 2023
47bcae0
자동 배포 설정
SeWooooong Mar 13, 2023
a9f4add
자동 배포 설정
SeWooooong Mar 14, 2023
4ae4e77
자동 배포 설정
SeWooooong Mar 14, 2023
370c443
자동 배포 설정
SeWooooong Mar 14, 2023
2e8aa69
자동 배포 설정
SeWooooong Mar 14, 2023
18652fe
자동 배포 설정
SeWooooong Mar 14, 2023
1dc1418
자동 배포 설정
SeWooooong Mar 14, 2023
6066db7
자동 배포 설정
SeWooooong Mar 14, 2023
3ba0973
자동 배포 설정
SeWooooong Mar 14, 2023
253e8f3
자동 배포 설정
SeWooooong Mar 14, 2023
13ed8e8
자동 배포 설정
SeWooooong Mar 14, 2023
3302b31
자동 배포 설정
SeWooooong Mar 14, 2023
64fbc14
자동 배포 설정
SeWooooong Mar 14, 2023
8b3ef11
자동 배포 설정
SeWooooong Mar 14, 2023
7dd94be
자동 배포 설정
SeWooooong Mar 14, 2023
a8455e6
자동 배포 설정
SeWooooong Mar 14, 2023
210a18e
자동 배포 설정
SeWooooong Mar 14, 2023
ca714ed
자동 배포 설정
SeWooooong Mar 14, 2023
c3eeaae
자동 배포 설정
SeWooooong Mar 14, 2023
5192d9f
자동 배포 설정
SeWooooong Mar 14, 2023
371c466
자동 배포 설정
SeWooooong Mar 14, 2023
f0a5571
자동 배포 설정
SeWooooong Mar 14, 2023
08365c0
자동 배포 설정
SeWooooong Mar 14, 2023
9f990f1
자동 배포 설정
SeWooooong Mar 14, 2023
c2a63f8
자동 배포 설정
SeWooooong Mar 14, 2023
0215989
자동 배포 설정
SeWooooong Mar 14, 2023
930d44a
자동 배포 설정
SeWooooong Mar 14, 2023
fd9c8b5
자동 배포 설정
SeWooooong Mar 14, 2023
121c104
자동 배포 설정
SeWooooong Mar 14, 2023
b8a578a
자동 배포 설정
SeWooooong Mar 14, 2023
cf7f66a
[FEATURE] 엔티티를 생성한다. (#9) (#10)
EUNJEONGMUN Mar 14, 2023
4ac8437
Readme.md 추가
SeWooooong Mar 14, 2023
ab4dec3
deploy.sh 주석 추가 (자동 배포 마무리)
SeWooooong Mar 16, 2023
bce3cbd
포트 번호 변경
SeWooooong Mar 18, 2023
847ff52
포트 번호 변경
SeWooooong Mar 18, 2023
c45b52a
포트 번호 변경
SeWooooong Mar 18, 2023
eb7040c
Merge branch 'main' into #14/record
SeWooooong Mar 18, 2023
3a33ae1
feature/ [#14]Record
SeWooooong Mar 18, 2023
32f25e1
feature/ Record[#14]
SeWooooong Mar 18, 2023
67fc83a
Record 에러처리
SeWooooong Mar 21, 2023
c317e29
Merge branch 'develop' into #14/record
SeWooooong Mar 21, 2023
66b35e5
feature / Record add API
SeWooooong Mar 21, 2023
245f9a3
feature / Record add API #14
SeWooooong Mar 21, 2023
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
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

group = 'com.todaysgym'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
sourceCompatibility = "11"

configurations {
compileOnly {
Expand Down Expand Up @@ -56,4 +56,5 @@ task copyGitSubmodule(type: Copy) {

jar {
enabled = false
}
}

12 changes: 11 additions & 1 deletion src/main/java/com/todaysgym/todaysgym/TodaysgymApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import javax.annotation.PostConstruct;
import java.util.Locale;
import java.util.TimeZone;

@SpringBootApplication
@EnableJpaAuditing
public class TodaysgymApplication {

@PostConstruct
public void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
Locale.setDefault(Locale.KOREA);
}
public static void main(String[] args) {
SpringApplication.run(TodaysgymApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@AllArgsConstructor
public enum RecordErrorCode implements ErrorCode {
EMPTY_RECORD("RECORD_001", "존재하지 않는 기록입니다."),
RECORD_DATE_EXISTS("RECORD_OO2", "기록은 하루에 한번만 추가가 가능합니다.")
;
private final String errorCode;
private final String message;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public class Member extends BaseTimeEntity {

@OneToMany(mappedBy = "member", cascade = {CascadeType.ALL}, orphanRemoval = true)
private List<Tag> tagList = new ArrayList<>();


public void addRecordCount() {this.recordCount++;}
public void updateRecordCheck(){this.recordCheck = true;}
}
22 changes: 22 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/record/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,26 @@ public class Record extends BaseTimeEntity {

@OneToMany(mappedBy = "record", cascade = {CascadeType.ALL}, orphanRemoval = true)
private List<Tag> tagList = new ArrayList<>();



public static Record createRecord(String content, Member user){
Record record = new Record();
record.createContent(content);
record.createMember(user);
return record;
}
public void createMember(Member member){this.member = member;}
public void createContent(String content){this.content = content;}

public void createPhotoList(RecordPhoto recordPhoto){
photoList.add(recordPhoto);
recordPhoto.createRecord(this);
}

public void addTagList(Tag tag){
tagList.add(tag);
tag.createRecord(this);
}

}
27 changes: 27 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/record/RecordController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.todaysgym.todaysgym.record;

import com.todaysgym.todaysgym.config.response.BaseResponse;
import com.todaysgym.todaysgym.record.dto.RecordGetReq;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class RecordController {
private final RecordService recordService;

/**
* 사진 여러개, 태그 여러개 저장
*/
@PostMapping("/record")
public BaseResponse<String> createRecord(@RequestPart(value = "image", required = false) List<MultipartFile> multipartFiles,
@RequestPart(value = "recordGetReq") @Valid RecordGetReq recordGetReq){
return new BaseResponse<>(recordService.saveRecord(multipartFiles, recordGetReq));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface RecordRepository extends JpaRepository<Record, Long> {
Optional<Record> getByRecordId(Long recordId);

@Query("select count(r) from Record r where FORMATDATETIME(r.createdAt, 'yyyy-MM-dd') = :date and r.member.memberId = :memberId")
Integer findByRecordDate(@Param("date") String date, @Param("memberId") Long memberId);

}
68 changes: 68 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/record/RecordService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.todaysgym.todaysgym.record;

import com.todaysgym.todaysgym.config.exception.BaseException;
import com.todaysgym.todaysgym.member.Member;
import com.todaysgym.todaysgym.record.dto.RecordGetReq;
import com.todaysgym.todaysgym.record.photo.RecordPhotoService;
import com.todaysgym.todaysgym.tag.TagService;
import com.todaysgym.todaysgym.utils.S3Service;
import com.todaysgym.todaysgym.utils.UtilService;
import com.todaysgym.todaysgym.utils.dto.getS3Res;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.todaysgym.todaysgym.config.exception.errorCode.RecordErrorCode.RECORD_DATE_EXISTS;

@Service
@RequiredArgsConstructor
public class RecordService {

private final RecordRepository recordRepository;
private final UtilService utilService;
private final S3Service s3Service;
private final RecordPhotoService recordPhotoService;
private final TagService tagService;
/**
* Record, photo, tag 저장
*/
@Transactional
public String saveRecord(List<MultipartFile> multipartFiles, RecordGetReq recordGetReq) throws BaseException {
validateDuplicateRecord();
//1. 엔티티 조회
Member member = utilService.findByMemberIdWithValidation(1L);
//2. 기록 내용, 사용자 추가
if(!member.isRecordCheck()) {
member.addRecordCount();
}
member.updateRecordCheck();
Record record = Record.createRecord(recordGetReq.getContent(), member);
recordRepository.save(record);
//3. 기록 사진 추가
if(multipartFiles != null) {
List<getS3Res> imgUrls = s3Service.uploadFile(multipartFiles);
recordPhotoService.saveAllRecordPhotoByRecord(imgUrls, record);
}
//4. 태크 추가
tagService.saveAllTagByRecord(recordGetReq, record);
return "기록을 추가했습니다";
}

/**
* 하루에 하나만 기록 추가
*/
private void validateDuplicateRecord() throws BaseException {
Member member = utilService.findByMemberIdWithValidation(1L);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Integer count = recordRepository.findByRecordDate(ZonedDateTime.now(ZoneId.of("Asia/Seoul")).format(formatter).toString() , member.getMemberId());
if(count > 0){
throw new BaseException(RECORD_DATE_EXISTS);
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/record/dto/RecordGetReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.todaysgym.todaysgym.record.dto;

import com.todaysgym.todaysgym.tag.Tag;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
public class RecordGetReq {
@NotNull
private String content;
private List<Tag> tags = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "record_photo")
Expand All @@ -30,4 +32,9 @@ public class RecordPhoto {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "record_id")
private Record record;


public void createRecord(Record record){
this.record = record;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.todaysgym.todaysgym.record.photo;

import com.todaysgym.todaysgym.record.Record;
import com.todaysgym.todaysgym.utils.S3Service;
import com.todaysgym.todaysgym.utils.dto.getS3Res;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class RecordPhotoService {

private final RecordPhotoRepository recordPhotoRepository;
private final S3Service s3Service;

@Transactional
public void saveRecordPhoto(List<RecordPhoto> recordPhotos){
recordPhotoRepository.saveAll(recordPhotos);
}

/**
*여러개의 recordPhoto 저장
*/
@Transactional
public void saveAllRecordPhotoByRecord(List<getS3Res> getS3ResList, Record record) {
List<RecordPhoto> recordPhotos = new ArrayList<>();
for(getS3Res getS3Res : getS3ResList){
RecordPhoto recordPhoto = RecordPhoto.builder().imgUrl(getS3Res.getImgUrl()).fileName(getS3Res.getFileName()).build();
recordPhotos.add(recordPhoto);
record.createPhotoList(recordPhoto);
}
saveRecordPhoto(recordPhotos);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public class Tag extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

public void createRecord(Record record){
this.record = record;
}
public void createMember(Member member){
this.member=member;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/tag/TagController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.todaysgym.todaysgym.tag;

public class TagController {
}
39 changes: 39 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/tag/TagService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.todaysgym.todaysgym.tag;

import com.todaysgym.todaysgym.record.Record;
import com.todaysgym.todaysgym.record.dto.RecordGetReq;
import com.todaysgym.todaysgym.utils.UtilService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@RequiredArgsConstructor
public class TagService {
private final TagRepository tagRepository;
private final UtilService utilService;
/**
* 태크 여러개 저장
*/
@Transactional
public void saveTag(List<Tag> tags){
tagRepository.saveAll(tags);
}


/**
* Record 연관된 태그 모두 저장
*/
@Transactional
public void saveAllTagByRecord(RecordGetReq recordGetReq, Record record) {
List<Tag> tags = recordGetReq.getTags();
for (Tag tag : tags) {
record.addTagList(tag);
tag.createMember(record.getMember());
}
saveTag(tags);
}
}
Loading