Skip to content

Commit

Permalink
Merge pull request #40 from goormthon-Univ/develop
Browse files Browse the repository at this point in the history
[develop] main merge
  • Loading branch information
JoongHyun-Kim committed Mar 23, 2024
2 parents ca44b45 + 59d20af commit 8847c29
Show file tree
Hide file tree
Showing 22 changed files with 640 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/hatcher/haemo/HaemoApplication.java
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 HaemoApplication {

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/hatcher/haemo/comment/dto/CommentDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hatcher.haemo.comment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;

public record CommentDto(Long commentIdx,
String writer,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd HH:mm", timezone = "Asia/Seoul")
LocalDateTime createdDate,
String content) {}
6 changes: 3 additions & 3 deletions src/main/java/com/hatcher/haemo/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

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

@Getter
@MappedSuperclass
Expand All @@ -22,8 +22,8 @@ public class BaseEntity {

@CreatedDate
@Column(updatable = false)
private LocalDate createdDate;
private LocalDateTime createdDate;

@LastModifiedDate
private LocalDate lastModifiedDate;
private LocalDateTime lastModifiedDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public class RequestURI {
public final static String user = "/users";
public final static String recruitment = "/recruitments";
public final static String notification = "/notifications";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,25 @@ public enum BaseResponseStatus {

// recruitment(2100-2199)
WRONG_RECRUIT_TYPE(false, HttpStatus.NOT_FOUND, "해당 Recruit type을 찾을 수 없습니다."),
INVALID_RECRUITMENT_IDX(false, HttpStatus.NOT_FOUND, "해당 recruitment idx로 recruitment를 찾을 수 없습니다."),
NO_RECRUITMENT_LEADER(false, HttpStatus.CONFLICT, "해당 recruitment의 leader가 아닙니다."),
BLANK_RECRUITMENT_NAME(false, HttpStatus.BAD_REQUEST, "recruitment name이 비었습니다."),
BLANK_RECRUITMENT_TYPE(false, HttpStatus.BAD_REQUEST, "recruitment type이 비었습니다."),
BLANK_PARTICIPANT_LIMIT(false, HttpStatus.BAD_REQUEST, "participant limit이 비었습니다."),
BLANK_CONTACT_URL(false, HttpStatus.BAD_REQUEST, "contact url이 비었습니다."),
BLANK_DESCRIPTION(false, HttpStatus.BAD_REQUEST, "description이 비었습니다."),
LARGER_THAN_CURRENT_PARTICIPANT(false, HttpStatus.CONFLICT, "입력하신 모집 인원이 현재 참여 인원보다 작습니다."),
NOT_RECRUITING_STATUS(false, HttpStatus.CONFLICT, "모집중 상태가 아닙니다."),
ALREADY_DONE_RECRUITMENT(false, HttpStatus.CONFLICT, "해당 띱은 이미 모집인원에 도달했습니다."),
LEADER_ROLE(false, HttpStatus.BAD_REQUEST, "리더는 띱 참여가 불가능합니다."),
NOT_LEADER_ROLE(false, HttpStatus.BAD_REQUEST, "해당 띱의 리더가 아닙니다."),
NOT_MEMBER_ROLE(false, HttpStatus.BAD_REQUEST, "해당 띱의 멤버가 아닙니다."),

// comment(2200-2299)

// notification(2300-2399)
INVALID_NOTIFICATION_IDX(false, HttpStatus.NOT_FOUND, "해당 notification idx로 notification을 찾을 수 없습니다."),


/**
* 3000: Response 오류
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.hatcher.haemo.notification.application;

import com.hatcher.haemo.common.BaseResponse;
import com.hatcher.haemo.common.exception.BaseException;
import com.hatcher.haemo.notification.domain.Notification;
import com.hatcher.haemo.notification.dto.NotificationDto;
import com.hatcher.haemo.notification.dto.NotificationListResponse;
import com.hatcher.haemo.notification.repository.NotificationRepository;
import com.hatcher.haemo.user.application.UserService;
import com.hatcher.haemo.user.domain.User;
import com.hatcher.haemo.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hatcher.haemo.common.constants.Constant.ACTIVE;
import static com.hatcher.haemo.common.enums.BaseResponseStatus.*;

@Service
@RequiredArgsConstructor
public class NotificationService {

private final UserRepository userRepository;
private final UserService userService;
private final NotificationRepository notificationRepository;

// 알림 목록 조회
public BaseResponse<NotificationListResponse> getNotificationList() throws BaseException {
try {
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
List<NotificationDto> notificationDtoList = notificationRepository.findByUserAndStatusEquals(user, ACTIVE).stream()
.map(notification -> {
long activeParticipantsCount = notification.getRecruitment().getParticipants().stream()
.filter(participant -> participant.getStatus().equals(ACTIVE)).count();
return new NotificationDto(notification.getNotificationIdx(), notification.getRecruitment().getName(), notification.getRecruitment().getContactUrl(),
notification.getRecruitment().getLeader().getNickname(), activeParticipantsCount+1,
notification.getRecruitment().getParticipantLimit(), notification.getRecruitment().getDescription());}).toList();
return new BaseResponse<>(new NotificationListResponse(notificationDtoList));
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

// 알림 삭제
public BaseResponse<String> deleteNotification(Long notificationIdx) throws BaseException {
try {
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Notification notification = notificationRepository.findById(notificationIdx).orElseThrow(() -> new BaseException(INVALID_NOTIFICATION_IDX));
notificationRepository.delete(notification);
notification.removeNotificationFromUser(user);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hatcher.haemo.notification.domain;

import com.hatcher.haemo.common.BaseEntity;
import com.hatcher.haemo.recruitment.domain.Recruitment;
import com.hatcher.haemo.user.domain.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

@Entity
@Getter
@DynamicInsert
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Notification extends BaseEntity {

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

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, name = "recruitment")
private Recruitment recruitment;

public void setUser(User user) {
this.user = user;
user.getNotifications().add(this);
}

public void removeNotificationFromUser(User user) {
this.user = user;
user.getNotifications().remove(this);
}

@Builder
public Notification(User user, Recruitment recruitment) {
this.user = user;
this.recruitment = recruitment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hatcher.haemo.notification.dto;

public record NotificationDto(Long notificationIdx,
String name,
String contactUrl,
String leader,
long participantNumber,
Integer participantLimit,
String description) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hatcher.haemo.notification.dto;

import java.util.List;

public record NotificationListResponse(List<NotificationDto> notificationList) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hatcher.haemo.notification.presentation;

import com.hatcher.haemo.common.BaseResponse;
import com.hatcher.haemo.common.exception.BaseException;
import com.hatcher.haemo.notification.application.NotificationService;
import com.hatcher.haemo.notification.dto.NotificationListResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.hatcher.haemo.common.constants.RequestURI.notification;


@RestController
@RequiredArgsConstructor
@RequestMapping(notification)
public class NotificationController {

private final NotificationService notificationService;

// 알림 목록 조회
@GetMapping("")
public BaseResponse<NotificationListResponse> getNotificationList() {
try {
return notificationService.getNotificationList();
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

// 알림 삭제
@DeleteMapping("/{notificationIdx}")
public BaseResponse<String> deleteNotification(@PathVariable Long notificationIdx) {
try {
return notificationService.deleteNotification(notificationIdx);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hatcher.haemo.notification.repository;

import com.hatcher.haemo.notification.domain.Notification;
import com.hatcher.haemo.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findByUserAndStatusEquals(User user, String status);
}
Loading

0 comments on commit 8847c29

Please sign in to comment.