Skip to content

Commit

Permalink
Merge branch 'master' into 11-푸드트럭-기능-web
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsong111 authored May 27, 2024
2 parents 5671dfe + 1dea200 commit c47c06c
Show file tree
Hide file tree
Showing 23 changed files with 532 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface EventRepository extends JpaRepository<Event, Long> {
void deleteByManager(User manager);

List<Event> findAllByStatus(EventStatus status);
List<Event> findAllByManager(User manager);
}
87 changes: 42 additions & 45 deletions src/main/java/ac/kr/deu/connect/luck/event/EventService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ac.kr.deu.connect.luck.event;

import ac.kr.deu.connect.luck.event.dto.EventDetailResponse;
import ac.kr.deu.connect.luck.event.dto.EventRequest;
import ac.kr.deu.connect.luck.event_application.EventApplication;
import ac.kr.deu.connect.luck.image.ImageUploader;
import ac.kr.deu.connect.luck.user.User;
import ac.kr.deu.connect.luck.user.UserRepository;
Expand Down Expand Up @@ -35,6 +37,21 @@ public List<EventDetailResponse> getEvents(EventStatus eventStatus) {
return events.stream().map(eventMapper::toEventResponse).toList();
}

/**
* email 로 이벤트 목록 조회
*
* @param email 이벤트 매니저 email
* @return 이벤트 목록
*/
public List<EventDetailResponse> getEvents(String email) {
List<Event> events;
User user = userRepository.findByEmail(email).orElseThrow(
() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다.")
);
events = eventRepository.findAllByManager(user);
return events.stream().map(eventMapper::toEventResponse).toList();
}

/**
* 이벤트 상세 조회
*
Expand All @@ -51,21 +68,15 @@ public EventDetailResponse getEvent(Long id) {
/**
* 이벤트 생성
*
* @param title 이벤트 제목
* @param content 이벤트 내용
* @param zipCode 우편번호
* @param streetAddress 도로명 주소
* @param detailAddress 상세 주소
* @param startAt 시작 일시
* 예시) 2021-08-01T00:00:00
* 형식) yyyy-MM-dd'T'HH:mm:ss
* @param endAt 종료 일시
* @param eventRequest 이벤트 생성 요청 폼
* @param multipartFile 이벤트 대표 이미지
* null이면 기본 이미지 사용
* @param managerEmail 매니저 이름
* @return 생성된 이벤트
*/
public Event createEvent(String title, String content, String zipCode, String streetAddress, String detailAddress, String startAt, String endAt, MultipartFile multipartFile, String managerEmail) {
public Event createEvent(EventRequest eventRequest, MultipartFile multipartFile, String managerEmail) {
Event eventSaved = eventMapper.toEvent(eventRequest);

User user = userRepository.findByEmail(managerEmail).orElseThrow(
() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다.")
);
Expand All @@ -74,37 +85,23 @@ public Event createEvent(String title, String content, String zipCode, String st
if (multipartFile != null) {
image = imageUploader.uploadImage(multipartFile).getData().getUrl();
}
Event event = Event.builder()
.title(title)
.content(content)
.zipCode(zipCode)
.streetAddress(streetAddress)
.detailAddress(detailAddress)
.startAt(LocalDateTime.parse(startAt))
.endAt(LocalDateTime.parse(endAt))
.imageUrl(image)
.status(EventStatus.OPEN_FOR_APPLICATION)
.manager(user)
.build();
return eventRepository.save(event);
eventSaved.setImageUrl(image);
eventSaved.setManager(user);
eventSaved.setStatus(EventStatus.OPEN_FOR_APPLICATION);

return eventRepository.save(eventSaved);
}

/**
* 이벤트 수정
*
* @param id 이벤트 UID
* @param title 이벤트 제목
* @param content 이벤트 내용
* @param zipCode 우편번호
* @param streetAddress 도로명주소
* @param detailAddress 상세주소
* @param startAt 시작 시간
* @param endAt 종료 시간
* @param eventRequest 이벤트 생성 요청 폼
* @param multipartFile 이벤트 대표 이미지
* @param managerEmail 이벤트 매니저 이메일
* @return 수정된 이벤트
*/
public Event updateEvent(Long id, String title, String content, String zipCode, String streetAddress, String detailAddress, String startAt, String endAt, MultipartFile multipartFile, String managerEmail) {
public Event updateEvent(Long id, EventRequest eventRequest, MultipartFile multipartFile, String managerEmail) {
Event findEvent = eventRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("해당 이벤트가 존재하지 않습니다.")
);
Expand All @@ -117,32 +114,32 @@ public Event updateEvent(Long id, String title, String content, String zipCode,
String image = imageUploader.uploadImage(multipartFile).getData().getUrl();
findEvent.setImageUrl(image);
}
if (title != null) {
findEvent.setTitle(title);
if (eventRequest.title() != null) {
findEvent.setTitle(eventRequest.title());
}

if (content != null) {
findEvent.setContent(content);
if (eventRequest.content() != null) {
findEvent.setContent(eventRequest.content());
}

if (zipCode != null) {
findEvent.setZipCode(zipCode);
if (eventRequest.zipCode() != null) {
findEvent.setZipCode(eventRequest.zipCode());
}

if (streetAddress != null) {
findEvent.setStreetAddress(streetAddress);
if (eventRequest.streetAddress() != null) {
findEvent.setStreetAddress(eventRequest.streetAddress());
}

if (detailAddress != null) {
findEvent.setDetailAddress(detailAddress);
if (eventRequest.detailAddress() != null) {
findEvent.setDetailAddress(eventRequest.detailAddress());
}

if (startAt != null) {
findEvent.setStartAt(LocalDateTime.parse(startAt));
if (eventRequest.startAt() != null) {
findEvent.setStartAt(eventRequest.startAt());
}

if (endAt != null) {
findEvent.setEndAt(LocalDateTime.parse(endAt));
if (eventRequest.endAt() != null) {
findEvent.setEndAt(eventRequest.endAt());
}
return eventRepository.save(findEvent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ac.kr.deu.connect.luck.event.controller;

import ac.kr.deu.connect.luck.event.EventService;
import ac.kr.deu.connect.luck.event.EventStatus;
import ac.kr.deu.connect.luck.event.dto.EventRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;

@Controller
@RequestMapping("/event")
Expand All @@ -17,14 +20,65 @@ public class EventController {

@GetMapping
public String getEvent(Model model) {
model.addAttribute("events", eventService.getEvents(null));
model.addAttribute("events", eventService.getEvents((EventStatus) null)); //나중에 인자 신청 가능한 이벤트로 바꿔야함
return "event/event-list";
}

@GetMapping("/my")
public String getMyFoodTruck(Model model, Principal principal) {
model.addAttribute("events", eventService.getEvents(principal.getName()));
return "event/my_event_list";
}

@GetMapping("/{id}")
public String getEventDetail(@PathVariable("id") Long id, Model model) {
model.addAttribute("event", eventService.getEvent(id));
return "event/event-form";
}

@GetMapping("/register")
public String getEventRegister(Model model) {
return "event/event_register";
}

@PostMapping("/create")
public String registerEventPost(
Principal principal,
EventRequest eventRequest, @RequestParam("multipartFile") MultipartFile multipartFile
){
eventService.createEvent(eventRequest, multipartFile, principal.getName());
return "redirect:/event/my";
}

@GetMapping("/delete/{id}")
public String getEventDelete(@PathVariable("id") Long id, Principal principal) {
eventService.deleteEvent(id, principal.getName());
return "redirect:/event/my";
}

@GetMapping("/edit/{id}")
public String getEventUpdate(@PathVariable("id") Long id, Model model) {
model.addAttribute("event", eventService.getEvent(id));
return "event/event_update";
}

@PostMapping("/update")
public String updateEventPost(
@RequestParam("eventId") Long id,
Principal principal,
EventRequest eventRequest,
@RequestParam("multipartFile") MultipartFile multipartFile
){
eventService.updateEvent(id, eventRequest, multipartFile, principal.getName());
return "redirect:/event/my";
}
@PostMapping("/statusUpdate/{id}")
public String updateEventPost(
@PathVariable("id") Long id,
Principal principal,
@RequestParam("status") EventStatus eventStatus
){
eventService.changeEventStatus(id, eventStatus, principal.getName());
return "redirect:/event/my";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public ResponseEntity<EventDetailResponse> getEvent(@PathVariable Long id) {
return ResponseEntity.ok(eventService.getEvent(id));
}


@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//서비스 로직 수정함 레스트도 수정되어야할듯
/*@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PreAuthorize("hasRole('ROLE_EVENT_MANAGER')")
@Operation(summary = "이벤트 생성", description = "이벤트 생성\n이벤트 주소입력 시 카카오 우편번호 서비스를 사용해서 주소를 입력받아야함.")
public ResponseEntity<Event> createEvent(
Expand Down Expand Up @@ -72,7 +72,7 @@ public ResponseEntity<Event> updateEvent(
@Parameter(description = "이벤트 대표 이미지") @RequestPart(value = "image", required = false) MultipartFile multipartFile,
Principal principal) {
return ResponseEntity.ok(eventService.updateEvent(id, title, content, zipCode, streetAddress, detailAddress, startAt, endAt, multipartFile, principal.getName()));
}
}*/


@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class EventApplication extends BaseEntity {
@ManyToOne
private Event event;

private Long foodTruckId;

@ManyToOne
private User foodTruckManager;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ac.kr.deu.connect.luck.event_application;
import ac.kr.deu.connect.luck.event.EventService;
import ac.kr.deu.connect.luck.user.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.security.Principal;

@Controller
@RequestMapping("/apply")
@RequiredArgsConstructor
public class EventApplicationController {

private final EventService eventService;
private final UserService userService;
private final EventApplicationService eventApplicationService;

@GetMapping("/{id}")
public String getEventDetail(@PathVariable("id") Long id, Model model, Principal principal) {
model.addAttribute("event", eventService.getEvent(id));
model.addAttribute("user", userService.findUserByEmail(principal.getName()));
return "event/event-apply";
}

//푸드트럭 id 이용해서 푸드트럭 사장 정보에서 푸드트럭 id에 맞는 푸드트럭 빼와서 푸드트럭 이미지랑 그런거 보여줘여할듯 아직 미구현
@GetMapping("/list/{id}")
public String getApplyList(@PathVariable("id") Long id, Model model, Principal principal) {
model.addAttribute("user", userService.findUserByEmail(principal.getName()));
model.addAttribute("applications", eventApplicationService.getEventApplications(id));
return "event/my_apply_list";
}

@PostMapping("/create")
public String registerFoodTruckPost(
Principal principal,
EventApplicationRequest eventApplicationRequest
){
eventApplicationService.createEventApplication(eventApplicationRequest, principal.getName());
return "redirect:/event";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

import io.swagger.v3.oas.annotations.media.Schema;

import io.swagger.v3.oas.annotations.media.Schema;

public record EventApplicationRequest(
@Schema(description = "이벤트 ID", example = "1")
Long eventId,

@Schema(description = "푸드트럭 ID", example = "2")
Long foodTruckId,

@Schema(description = "신청 메시지", example = "죠희 타코야키 잘 만들어요")
String comment) {
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ public List<EventApplication> getMyEventApplicationsForEventManager(
return eventApplicationService.getMyEventApplicationsForEventManager(eventId, principal.getName());
}

@PostMapping("/application")
//서비스 로직 수정했는데 Rest 로직은 수정 못하겠음 남주 SOS 바람
/*@PostMapping("/application")
@PreAuthorize("hasRole('ROLE_FOOD_TRUCK_MANAGER')")
@Operation(summary = "이벤트 신청", description = "푸드트럭 매니저가 이벤트에 신청하는 API<br>푸드트럭 매니저만 사용 가능합니다")
public ResponseEntity<EventApplication> applyEvent(
@Parameter(description = "이벤트 UID") @RequestParam(name = "eventId") Long id,
@RequestBody EventApplicationRequest eventApplicationRequest,
Principal principal) {
return ResponseEntity.ok(eventApplicationService.createEventApplication(eventApplicationRequest, id, principal.getName()));
}
}*/


@PostMapping("/event/{eventId}/applications/{applicationId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ public List<EventApplication> getMyEventApplicationsForEventManager(Long eventId
* 지원서 신규 작성 메소드
*
* @param eventApplicationRequest 이벤트 요청 폼
* @param eventId 이벤트 키
* @param email 신청자(푸드트럭 매니저) 이메일
* @return 생성된 신규 지원서
*/
public EventApplication createEventApplication(EventApplicationRequest eventApplicationRequest, Long eventId, String email) {
public EventApplication createEventApplication(EventApplicationRequest eventApplicationRequest, String email) {
EventApplication eventApplicationSaved = eventApplicationMapper.toEventApplication(eventApplicationRequest);

// 유저 정보 조회
Expand All @@ -66,10 +65,10 @@ public EventApplication createEventApplication(EventApplicationRequest eventAppl
throw new CustomException(CustomErrorCode.USER_ID_NOT_MATCH);
}

Event evnet = eventRepository.findById(eventId).orElseThrow(() -> new CustomException(CustomErrorCode.EVENT_NOT_FOUND));
Event event = eventRepository.findById(eventApplicationRequest.eventId()).orElseThrow(() -> new CustomException(CustomErrorCode.EVENT_NOT_FOUND));

eventApplicationSaved.setFoodTruckManager(user);
eventApplicationSaved.setEvent(evnet);
eventApplicationSaved.setEvent(event);
eventApplicationSaved.setStatus(ApplicationStatus.PENDING);

return eventApplicationRepository.save(eventApplicationSaved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -89,5 +90,6 @@ public String editFoodTruckReview(
) {
foodTruckReviewService.saveFoodTruckReview(id, foodTruckReview, principal.getName(), reviewId);
return "redirect:/user";
}
}
}

Loading

0 comments on commit c47c06c

Please sign in to comment.