Skip to content

Commit

Permalink
Merge pull request #9 from Kusitms-28th-Meet-Up-B/feature/#1/mainAPI
Browse files Browse the repository at this point in the history
feat : 최신프로그램 조회, hot fix : 유저 테이블 생성 오류 수정
  • Loading branch information
MyunghyunNero authored Oct 27, 2023
2 parents 0ac4e05 + 90a8884 commit e93687b
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 8 deletions.
26 changes: 26 additions & 0 deletions src/main/java/kusitms/gallae/controller/ProgramController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kusitms.gallae.controller;

import kusitms.gallae.config.BaseResponse;
import kusitms.gallae.dto.program.ProgramMainRes;
import kusitms.gallae.service.ProgramService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/program")
public class ProgramController {

private final ProgramService programService;

@GetMapping("/recent")
public ResponseEntity<BaseResponse<List<ProgramMainRes>>> findRecentProgram(){
return ResponseEntity.ok(new BaseResponse<>(this.programService.getRecentPrograms()));
}
}
91 changes: 91 additions & 0 deletions src/main/java/kusitms/gallae/domain/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package kusitms.gallae.domain;


import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

@Entity
@Table(name = "program")
@Setter
@Getter
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class Program {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_Id", nullable = false)
@ToString.Exclude
private User user;

private String programName;

private String location;

private LocalDateTime recruitStartDate;

private LocalDateTime recruitEndDate;

private LocalDateTime tripStartDate;

private LocalDateTime tripEndDate;

private LocalDateTime activeStartDate;

private LocalDateTime activeEndDate;

private String contact;

private String contactNumber;

private String programLink;

private String programType;

private String detailType;

private String photoUrl;

private Long programLike;

@Column
@CreationTimestamp
private LocalDateTime createdAt;

@Column
@UpdateTimestamp
private LocalDateTime updatedAt;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private ProgramStatus status;

public enum ProgramStatus {
ACTIVE,
SAVE, // 임시 저장
FINISH, //마감
DELETED
}

public void delete() {
this.status = ProgramStatus.DELETED;
}

public void finish(){
this.status = ProgramStatus.FINISH;
}

public void save(){
this.status = ProgramStatus.SAVE;
}

}
30 changes: 22 additions & 8 deletions src/main/java/kusitms/gallae/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(length = 45, nullable = false)
private String username;
private String nickName;

@Column(length = 45, nullable = false)
private String loginId;

@Column(length = 500, nullable = false)
private String passward;
private String loginPw;

@Column(length = 500, nullable = false)
private String refreshToken;

@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "ENUM")
private Role role;
@Column(nullable = false)
private UserSignUpStatus signUpStatus;

@CreationTimestamp
@Column
Expand All @@ -51,7 +47,25 @@ public class User {
@Column
private LocalDateTime deletedAt;

public enum UserSignUpStatus{
MANAGER,
USER,
ADMIN
}

public void renewRefreshToken() {
this.refreshToken = RandomStringUtils.randomAlphanumeric(32);
}

public Role getRole(){
if(this.signUpStatus == UserSignUpStatus.USER ) {
return Role.USER;
}else if(this.signUpStatus == UserSignUpStatus.MANAGER) {
return Role.MANAGER;
}else if(this.signUpStatus == UserSignUpStatus.ADMIN) {
return Role.ADMIN;
}else{
throw new IllegalArgumentException();
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/kusitms/gallae/dto/program/ProgramMainRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kusitms.gallae.dto.program;


import lombok.Data;

@Data
public class ProgramMainRes {

private String photoUrl;

private String programName;

private Long Like;

private String remainDay;

}
19 changes: 19 additions & 0 deletions src/main/java/kusitms/gallae/global/DurationCalcurator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kusitms.gallae.global;

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

public class DurationCalcurator {
public static String getDuration(LocalDateTime endDate){
LocalDateTime today = LocalDate.now().atStartOfDay();
LocalDateTime dateTime = LocalDate.of(endDate.getYear(), endDate.getMonth() , endDate.getDayOfMonth())
.atStartOfDay();
Long remainDay = Duration.between(today,dateTime).toDays();
if(remainDay > 0) {
return String.valueOf(remainDay);
}else{
return "마감";
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/kusitms/gallae/repository/ProgramRespository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kusitms.gallae.repository;

import kusitms.gallae.domain.Program;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface ProgramRespository extends JpaRepository<Program, Long> {
@Override
Optional<Program> findById(Long programId);

List<Program> findByProgramType(String programType);

List<Program> findTop4ByOrderByCreatedAtDesc();
}
10 changes: 10 additions & 0 deletions src/main/java/kusitms/gallae/service/ProgramService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kusitms.gallae.service;

import kusitms.gallae.dto.program.ProgramMainRes;

import java.util.List;

public interface ProgramService {

List<ProgramMainRes> getRecentPrograms();
}
35 changes: 35 additions & 0 deletions src/main/java/kusitms/gallae/service/ProgramServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kusitms.gallae.service;


import jakarta.transaction.Transactional;
import kusitms.gallae.global.DurationCalcurator;
import kusitms.gallae.domain.Program;
import kusitms.gallae.dto.program.ProgramMainRes;
import kusitms.gallae.repository.ProgramRespository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Transactional
@Service
public class ProgramServiceImpl implements ProgramService {
private final ProgramRespository programRespository;

@Override
public List<ProgramMainRes> getRecentPrograms(){
List<Program> programs = programRespository.findTop4ByOrderByCreatedAtDesc();

return programs.stream().map(program -> {
ProgramMainRes programMainRes = new ProgramMainRes();
programMainRes.setProgramName(program.getProgramName());
programMainRes.setLike(program.getProgramLike());
programMainRes.setPhotoUrl(program.getPhotoUrl());
String strRemainDay = DurationCalcurator.getDuration(program.getRecruitEndDate());
programMainRes.setRemainDay(strRemainDay);
return programMainRes;
}).collect(Collectors.toList());
}
}
10 changes: 10 additions & 0 deletions src/test/java/kusitms/gallae/JwtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public class JwtTest {
Expand All @@ -22,4 +25,11 @@ void createJwt(){
//then
Assertions.assertNotNull(jwtToken);
}

@Test
void DurationTest(){
LocalDateTime today = LocalDate.now().atStartOfDay();
LocalDateTime dateTime = LocalDate.of(2023, 10 ,31).atStartOfDay();
Assertions.assertEquals(Duration.between(today,dateTime).toDays(),4);
}
}

0 comments on commit e93687b

Please sign in to comment.