Skip to content

Commit

Permalink
Merge pull request #19 from Kusitms-28th-Meet-Up-B/feature/manager
Browse files Browse the repository at this point in the history
관리자 대시보드 API 생성
  • Loading branch information
sebbbin authored Nov 17, 2023
2 parents 93ead46 + eafa2ea commit 5c9619f
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 4 deletions.
108 changes: 108 additions & 0 deletions src/main/java/kusitms/gallae/controller/ManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Positive;
import kusitms.gallae.config.BaseResponse;
import kusitms.gallae.config.BaseResponseStatus;
import kusitms.gallae.domain.Program;
import kusitms.gallae.dto.model.PostModel;
import kusitms.gallae.dto.program.ProgramDetailRes;
import kusitms.gallae.dto.program.ProgramManagerReq;
import kusitms.gallae.dto.program.ProgramPageMangagerRes;
import kusitms.gallae.dto.program.ProgramPostReq;
import kusitms.gallae.global.S3Service;
import kusitms.gallae.service.admin.ManagerService;
import kusitms.gallae.service.program.ProgramService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -103,6 +111,77 @@ public ResponseEntity<BaseResponse> saveProgram(
return ResponseEntity.ok(new BaseResponse<>(BaseResponseStatus.SUCCESS));
}

@Operation(summary = "프로그램 수정할 정보 가져오기", description = """
프로그램 정보를 가져옵니다.
프로그램 수정을 할 때 해당 메서드로 정보를 가져오고
postProgram으로 저장하시면 됩니다.
""")
@GetMapping("/program")
public ResponseEntity<BaseResponse<ProgramDetailRes>> findProgramDetail(
@Parameter(description = "프로그램 ID")
@RequestParam(value = "id", required = true) Long id
){
return ResponseEntity.ok(new BaseResponse<>(this.managerService.getProgramDetail(id)));
}

@Operation(summary = "관리자 프로그램중 진행되고 있는 정보들 가져오기", description = """
진행하고 있는 프로그램 입니다.
전체로 설정할경우 null로 보내주시면 됩니다.
""")
@GetMapping("/progressPrograms")
public ResponseEntity<BaseResponse<ProgramPageMangagerRes>> findProgramManagerProgress(
@Parameter(description = "프로그램 타입")
@RequestParam(value = "programType", required = false)
String programType,

@Parameter(description = "페이지 번호")
@Positive(message = "must be greater than 0")
@RequestParam(value = "page", defaultValue = "0")
Integer pageNumber,

@Parameter(description = "페이징 사이즈 (최대 100)")
@Min(value = 1, message = "must be greater than or equal to 1")
@Max(value = 100, message = "must be less than or equal to 100")
@RequestParam(value = "size", defaultValue = "20")
Integer pagingSize
){
ProgramManagerReq programManagerReq = new ProgramManagerReq();
programManagerReq.setProgramType(programType);
programManagerReq.setStatus(Program.ProgramStatus.SAVE);
PageRequest pageRequest = PageRequest.of(pageNumber,pagingSize);
programManagerReq.setPageable(pageRequest);
return ResponseEntity.ok(new BaseResponse<>(this.managerService.getManagerPrograms(programManagerReq)));
}

@Operation(summary = "관리자 프로그램 중 마감된 정보들 가져오기", description = """
진행하고 있는 프로그램 입니다.
전체로 설정할경우 null로 보내주시면 됩니다.
""")
@GetMapping("/finishPrograms")
public ResponseEntity<BaseResponse<ProgramPageMangagerRes>> findProgramManagerFinish(
@Parameter(description = "프로그램 유형")
@RequestParam(value = "programType", required = false)
String programType,

@Parameter(description = "페이지 번호")
@Positive(message = "must be greater than 0")
@RequestParam(value = "page", defaultValue = "0")
Integer pageNumber,

@Parameter(description = "페이징 사이즈 (최대 100)")
@Min(value = 1, message = "must be greater than or equal to 1")
@Max(value = 100, message = "must be less than or equal to 100")
@RequestParam(value = "size", defaultValue = "20")
Integer pagingSize
){
ProgramManagerReq programManagerReq = new ProgramManagerReq();
programManagerReq.setProgramType(programType);
programManagerReq.setStatus(Program.ProgramStatus.FINISH);
PageRequest pageRequest = PageRequest.of(pageNumber,pagingSize);
programManagerReq.setPageable(pageRequest);
return ResponseEntity.ok(new BaseResponse<>(this.managerService.getManagerPrograms(programManagerReq)));
}

@Operation(summary = "프로그램 임시저장", description = """
프로그램 저장을 합니다.
다른 API와 다르게 파일과 json Data를 구분해야합니다.
Expand Down Expand Up @@ -163,4 +242,33 @@ public ResponseEntity<BaseResponse<ProgramPostReq>> findTempProgram() {
//사용자 로그인 들어오면
return ResponseEntity.ok(new BaseResponse<>(this.managerService.getTempProgram()));
}



@Operation(summary = "프로그램 마감", description = """
프로그램을 마감하는 버튼 클릭시
프로그램 상태가 마감으로 변경됨
""")
@PostMapping("/finish")
public ResponseEntity<BaseResponse> finishProgram(
@Parameter(description = "프로그램 ID")
@RequestParam(value = "id", required = true) Long programId
) {
managerService.finishProgram(programId);
return ResponseEntity.ok(new BaseResponse<>(BaseResponseStatus.SUCCESS));
}

@Operation(summary = "프로그램 삭제", description = """
프로그램을 삭제합니다.
""")
@DeleteMapping("/deleteProgram")
public ResponseEntity<BaseResponse> deleteProgram(
@Parameter(description = "프로그램 Id")
@RequestParam(value = "programId", required = true)
Long programId
) {
//사용자 로그인 들어오면
managerService.deleteProgram(programId);
return ResponseEntity.ok(new BaseResponse<>(BaseResponseStatus.SUCCESS));
}
}
19 changes: 19 additions & 0 deletions src/main/java/kusitms/gallae/dto/program/ProgramManagerReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kusitms.gallae.dto.program;


import kusitms.gallae.domain.Program;
import kusitms.gallae.domain.User;
import lombok.Data;
import org.springframework.data.domain.Pageable;

@Data
public class ProgramManagerReq {

private String programType;

private Program.ProgramStatus status;

private User user;

private Pageable pageable;
}
16 changes: 16 additions & 0 deletions src/main/java/kusitms/gallae/dto/program/ProgramManagerRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kusitms.gallae.dto.program;


import lombok.Data;

import java.time.LocalDate;

@Data
public class ProgramManagerRes {
private Long id;
private String title;
private Long viewCount;
private Long like;
private LocalDate recruitStartDate;
private LocalDate recuritEndDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kusitms.gallae.dto.program;


import lombok.Data;

import java.util.List;

@Data
public class ProgramPageMangagerRes {

private Integer totalSize;
private List<ProgramManagerRes> programs;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package kusitms.gallae.repository.program;

import kusitms.gallae.domain.Program;
import kusitms.gallae.dto.program.ProgramManagerReq;
import kusitms.gallae.dto.program.ProgramSearchReq;
import org.springframework.data.domain.Page;

public interface ProgramRepositoryCustom {

Page<Program> getDynamicSearch(ProgramSearchReq programSearchReq);

Page<Program> getDynamicMananger(ProgramManagerReq programManagerReq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.querydsl.core.types.dsl.Wildcard;
import com.querydsl.jpa.impl.JPAQueryFactory;
import kusitms.gallae.domain.Program;
import kusitms.gallae.domain.User;
import kusitms.gallae.dto.program.ProgramManagerReq;
import kusitms.gallae.dto.program.ProgramSearchReq;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -43,6 +45,40 @@ public Page<Program> getDynamicSearch(ProgramSearchReq programSearchReq){
return new PageImpl<>(programs, programSearchReq.getPageable(), Objects.requireNonNull(totalSize));
}


public Page<Program> getDynamicMananger(ProgramManagerReq programManagerReq){
List<Program> programs = this.jpaQueryFactory
.selectFrom(program)
.where(createManagerProgramCondition(programManagerReq))
.orderBy(new OrderSpecifier<>(Order.DESC,program.createdAt))
.offset(programManagerReq.getPageable().getOffset())
.limit(programManagerReq.getPageable().getPageSize())
.fetch();

Long totalSize = this.jpaQueryFactory
.select(Wildcard.count)
.from(program)
.where(createManagerProgramCondition(programManagerReq))
.fetchOne();
return new PageImpl<>(programs, programManagerReq.getPageable(), Objects.requireNonNull(totalSize));
}

private BooleanBuilder createManagerProgramCondition(ProgramManagerReq programManagerReq) {
BooleanBuilder booleanBuilder = new BooleanBuilder();

if(programManagerReq.getProgramType() != null) {
booleanBuilder.and(program.programType.contains(programManagerReq.getProgramType()));
}

if(programManagerReq.getStatus() != null) {
booleanBuilder.and(program.status.eq(programManagerReq.getStatus()));
}

booleanBuilder.and(program.user.eq(programManagerReq.getUser()));

return booleanBuilder;
}

private BooleanBuilder createSearchCondition(ProgramSearchReq programSearchReq) {
BooleanBuilder booleanBuilder = new BooleanBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface ProgramRespository extends JpaRepository<Program, Long> {
List<Program> findTop4ByLocationContainingAndProgramTypeContainingAndStatus(String Location, String programType
, Program.ProgramStatus programStatus);

void deleteByIdAndStatus(Long programId,Program.ProgramStatus programStatus);

}
10 changes: 9 additions & 1 deletion src/main/java/kusitms/gallae/service/admin/ManagerService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.gallae.service.admin;

import kusitms.gallae.dto.program.ProgramPostReq;
import kusitms.gallae.dto.program.*;

public interface ManagerService {

Expand All @@ -11,5 +11,13 @@ public interface ManagerService {

ProgramPostReq getTempProgram();

ProgramDetailRes getProgramDetail(Long id);

ProgramPageMangagerRes getManagerPrograms(ProgramManagerReq programManagerReq);

void deleteTempProgram(Long programId);

void deleteProgram(Long programId);

void finishProgram(Long programId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package kusitms.gallae.service.admin;

import jakarta.transaction.Transactional;
import kusitms.gallae.config.BaseException;
import kusitms.gallae.config.BaseResponseStatus;
import kusitms.gallae.domain.Program;
import kusitms.gallae.domain.User;
import kusitms.gallae.dto.program.ProgramPostReq;
import kusitms.gallae.dto.program.*;
import kusitms.gallae.global.DurationCalcurator;
import kusitms.gallae.global.S3Service;
import kusitms.gallae.repository.program.ProgramRepositoryCustom;
import kusitms.gallae.repository.program.ProgramRepositoryImpl;
import kusitms.gallae.repository.program.ProgramRespository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
Expand All @@ -17,8 +28,35 @@ public class ManagerServiceImpl implements ManagerService {

private final ProgramRespository programRespository;

private final ProgramRepositoryCustom programRepositoryCustom;

private final S3Service s3Service;


@Override
public ProgramDetailRes getProgramDetail(Long id){
Program program = programRespository.findById(id).orElse(null);
if(program == null) {
throw new BaseException(BaseResponseStatus.BAD_REQUEST);
}else{
ProgramDetailRes programDetailRes = new ProgramDetailRes();
programDetailRes.setId(program.getId());
programDetailRes.setProgramName(program.getProgramName());
programDetailRes.setProgramLink(program.getProgramLink());
programDetailRes.setContact(program.getContact());
programDetailRes.setContactNumber(program.getContactNumber());
programDetailRes.setDescription(program.getDescription());
programDetailRes.setLocation(program.getLocation());
programDetailRes.setActiveStartDate(program.getActiveStartDate());
programDetailRes.setActiveEndDate(program.getActiveEndDate());
programDetailRes.setRecruitStartDate(program.getRecruitStartDate());
programDetailRes.setRecruitEndDate(program.getRecruitEndDate());
programDetailRes.setTripStartDate(program.getTripStartDate());
programDetailRes.setTripEndDate(program.getTripEndDate());
return programDetailRes;
}
}

@Override
public ProgramPostReq getTempProgram() {
Program tempProgram = programRespository.findByUserIdAndStatus(1L, Program.ProgramStatus.TEMP);
Expand Down Expand Up @@ -83,9 +121,46 @@ public void postTempProgram(ProgramPostReq programPostReq) {

@Override
public void deleteTempProgram(Long programId) {
programRespository.deleteByIdAndStatus(programId, Program.ProgramStatus.TEMP);
}

@Override
public ProgramPageMangagerRes getManagerPrograms(ProgramManagerReq programManagerReq) {
User user = new User();
user.setId(1L);
programManagerReq.setUser(user);
Page<Program> programs = programRepositoryCustom.getDynamicMananger(programManagerReq);
List<Program> pageToListManagerPrograms = programs.getContent();
ProgramPageMangagerRes programPageMangagerRes = new ProgramPageMangagerRes();
programPageMangagerRes.setPrograms(getProgramManagerRes(pageToListManagerPrograms));
programPageMangagerRes.setTotalSize(programs.getTotalPages());
return programPageMangagerRes;
}

@Override
public void finishProgram(Long programId) {
Program program = programRespository.findById(programId).orElse(null);
program.setStatus(Program.ProgramStatus.FINISH);
}

@Override
public void deleteProgram(Long programId) {
programRespository.deleteById(programId);
}

private List<ProgramManagerRes> getProgramManagerRes(List<Program> programs){
return programs.stream().map(program -> {
ProgramManagerRes programManagerRes = new ProgramManagerRes();
programManagerRes.setId(program.getId());
programManagerRes.setTitle(program.getProgramName());
programManagerRes.setLike(program.getProgramLike());
programManagerRes.setViewCount(program.getViewCount());
programManagerRes.setRecruitStartDate(program.getRecruitStartDate());
programManagerRes.setRecuritEndDate(program.getRecruitEndDate());
return programManagerRes;
}).collect(Collectors.toList());
}

private Program getProgramEntity(Program program ,ProgramPostReq programPostReq) {
program.setProgramName(programPostReq.getProgramName());
if(program.getPhotoUrl()==null) {
Expand Down
Loading

0 comments on commit 5c9619f

Please sign in to comment.