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

Issue#1 증상기록 관련 기능 구현 #3

Merged
merged 10 commits into from
Nov 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

@EnableJpaAuditing
@SpringBootApplication
public class NullnullteamApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hackathon.nullnullteam.global.constants;

import java.time.LocalDate;
import java.time.LocalDateTime;

public final class DateConstants {

public static final LocalDateTime DEFAULT_START_DATE = LocalDate.of(1990, 1, 1).atStartOfDay();

private DateConstants() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
@Getter
public abstract class BaseTimeEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdDate;
@Column(name = "created_at", columnDefinition = "DATETIME(0)")
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime modifiedDate;
@Column(name = "modified_at", columnDefinition = "DATETIME(0)")
private LocalDateTime modifiedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SymptomRecord extends BaseTimeEntity {
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user")
@JoinColumn(name = "member_id")
private Member member;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hackathon.nullnullteam.symptomrecord.controller;

import com.hackathon.nullnullteam.global.dto.GlobalResponse;
import com.hackathon.nullnullteam.global.dto.PagingResponse;
import com.hackathon.nullnullteam.symptomrecord.controller.dto.SymptomRecordRequest;
import com.hackathon.nullnullteam.symptomrecord.controller.dto.SymptomRecordResponse;
import com.hackathon.nullnullteam.symptomrecord.service.SymptomRecordService;
import com.hackathon.nullnullteam.symptomrecord.service.dto.SymptomRecordModel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/symptom")
public class SymptomRecordController {

private final SymptomRecordService symptomRecordService;

@PostMapping("")
public GlobalResponse addSymptomRecord(
Long memberId,
@RequestBody SymptomRecordRequest.Add request
){
symptomRecordService.addSymptomRecord(memberId, request.toCommand());

return GlobalResponse.builder().message("증상 기록 추가가 완료되었습니다.").build();

}

@GetMapping("/record")
public PagingResponse<SymptomRecordResponse.Info> getAllSymptomRecord(
Long memberId,
@PageableDefault(page = 0, size = 10, sort = "startDate", direction = Sort.Direction.ASC) Pageable pageable
){
Page<SymptomRecordModel.Info> allSymptomRecord = symptomRecordService.getAllSymptomRecord(memberId, pageable);

SymptomRecordResponse.Infos infoList = SymptomRecordResponse.Infos.from(allSymptomRecord);

return PagingResponse.from(infoList.infos());

}

@GetMapping("/record/monthly")
public PagingResponse<SymptomRecordResponse.Info> getMonthlySymptomRecord(
Long memberId,
@PageableDefault(page = 0, size = 10) Pageable pageable,
@RequestParam(name = "date", required = false) LocalDate date
) {
Page<SymptomRecordModel.Info> montlySymptomRecord = symptomRecordService.getMontlySymptomRecord(memberId, date, pageable);

SymptomRecordResponse.Infos infoList = SymptomRecordResponse.Infos.from(montlySymptomRecord);

return PagingResponse.from(infoList.infos());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hackathon.nullnullteam.symptomrecord.controller.dto;

import com.hackathon.nullnullteam.symptomrecord.service.dto.SymptomRecordCommand;
import lombok.Builder;

import java.time.LocalDate;

public class SymptomRecordRequest {

@Builder
public record Add(
String description,
String symptomName,
LocalDate startDate
){
public SymptomRecordCommand.Add toCommand(){
return SymptomRecordCommand.Add.builder()
.description(description)
.symptomName(symptomName)
.startDate(startDate)
.build();
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hackathon.nullnullteam.symptomrecord.controller.dto;

import com.hackathon.nullnullteam.symptomrecord.service.dto.SymptomRecordModel;
import lombok.Builder;
import org.springframework.data.domain.Page;

import java.time.LocalDate;

public class SymptomRecordResponse {

@Builder
public record Info(
Long userId,
LocalDate startDate,
String description,
String symptomName
){
public static SymptomRecordResponse.Info from(SymptomRecordModel.Info model) {
return Info.builder()
.userId(model.userId())
.startDate(model.startDate())
.description(model.description())
.symptomName(model.symptomName())
.build();
}

}

@Builder
public record Infos(
Page<SymptomRecordResponse.Info> infos
){
public static SymptomRecordResponse.Infos from(Page<SymptomRecordModel.Info> infoList){
return Infos.builder()
.infos(infoList.map(SymptomRecordResponse.Info::from))
.build();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.hackathon.nullnullteam.symptomrecord.infrastructure.repository;


import com.hackathon.nullnullteam.member.Member;
import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;

public interface SymptomRecordRepository extends JpaRepository<SymptomRecord, Long> {

Page<SymptomRecord> getAllByMember(Member member, Pageable pageable);

@Query("SELECT s FROM SymptomRecord s WHERE s.member = :member AND s.createdAt BETWEEN :startDate AND :endDate ORDER BY s.createdAt DESC")
Page<SymptomRecord> getAllByMemberAndCreatedAtBetween(@Param("member") Member member, @Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.hackathon.nullnullteam.symptomrecord.service;


import com.hackathon.nullnullteam.member.Member;
import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import com.hackathon.nullnullteam.symptomrecord.infrastructure.repository.SymptomRecordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class SymptomRecordReaderService {

private final SymptomRecordRepository symptomRecordRepository;

@Transactional(readOnly = true)
public Page<SymptomRecord> getSymptomListByMember(Member member, Pageable pageable){
return symptomRecordRepository.getAllByMember(member, pageable);
}

@Transactional(readOnly = true)
public Page<SymptomRecord> getMonthlySymptomListByMemberId(Member member, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable){
return symptomRecordRepository.getAllByMemberAndCreatedAtBetween(member, startDate, endDate, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.hackathon.nullnullteam.symptomrecord.service;

import com.hackathon.nullnullteam.global.constants.DateConstants;
import com.hackathon.nullnullteam.member.Member;
import com.hackathon.nullnullteam.member.service.MemberReaderService;
import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import com.hackathon.nullnullteam.symptomrecord.service.dto.SymptomRecordCommand;
import com.hackathon.nullnullteam.symptomrecord.service.dto.SymptomRecordModel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@RequiredArgsConstructor
public class SymptomRecordService {

private final MemberReaderService memberReaderService;
private final SymptomRecordReaderService symptomRecordReaderService;
private final SymptomRecordWriterService symptomRecordWriterService;

@Transactional
public void addSymptomRecord(Long memberId, SymptomRecordCommand.Add command){
Member member = memberReaderService.getMemberById(memberId);

SymptomRecord symptomRecord = command.toEntity(member);

symptomRecordWriterService.save(symptomRecord);
}

@Transactional(readOnly = true)
public Page<SymptomRecordModel.Info> getAllSymptomRecord(Long memberId, Pageable pageable){
Member member = memberReaderService.getMemberById(memberId);

Page<SymptomRecord> symptomRecordPage = symptomRecordReaderService.getSymptomListByMember(member, pageable);

return symptomRecordPage.map(SymptomRecordModel.Info::from);

}

@Transactional(readOnly = true)
public Page<SymptomRecordModel.Info> getMontlySymptomRecord(Long memberId, LocalDate date, Pageable pageable){
Member member = memberReaderService.getMemberById(memberId);

LocalDateTime startDate;
LocalDateTime endDate;

if (date == null) {
startDate = DateConstants.DEFAULT_START_DATE;
endDate = LocalDateTime.now();
} else {
startDate = date.withDayOfMonth(1).atStartOfDay();
endDate = date.withDayOfMonth(date.lengthOfMonth()).atTime(LocalTime.MAX);
}
Page<SymptomRecord> symptomRecordList = symptomRecordReaderService.getMonthlySymptomListByMemberId(member, startDate, endDate, pageable);
return symptomRecordList.map(SymptomRecordModel.Info::from);

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hackathon.nullnullteam.symptomrecord.service;


import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import com.hackathon.nullnullteam.symptomrecord.infrastructure.repository.SymptomRecordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class SymptomRecordWriterService {

private final SymptomRecordRepository symptomRecordRepository;

@Transactional
public void save(SymptomRecord symptomRecord){
symptomRecordRepository.save(symptomRecord);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hackathon.nullnullteam.symptomrecord.service.dto;

import com.hackathon.nullnullteam.member.Member;
import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import lombok.Builder;

import java.time.LocalDate;

public class SymptomRecordCommand {

@Builder
public record Add(
String description,
String symptomName,
LocalDate startDate
){
public SymptomRecord toEntity(Member member){
return SymptomRecord.builder()
.symptomName(symptomName)
.description(description)
.startDate(startDate)
.member(member)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hackathon.nullnullteam.symptomrecord.service.dto;

import com.hackathon.nullnullteam.symptomrecord.SymptomRecord;
import lombok.Builder;

import java.time.LocalDate;

public class SymptomRecordModel {

@Builder
public record Info(
Long userId,
LocalDate startDate,
String description,
String symptomName
){
public static SymptomRecordModel.Info from(SymptomRecord symptomRecord){
return Info.builder()
.userId(symptomRecord.getMember().getId())
.startDate(symptomRecord.getStartDate())
.description(symptomRecord.getDescription())
.symptomName(symptomRecord.getSymptomName())
.build();
}

}
}
Loading