-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from sopt-makers/sohyeon_#172
[FEAT] 알림 어드민 API 개발
- Loading branch information
Showing
12 changed files
with
316 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/sopt/makers/operation/controller/web/AlarmController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.sopt.makers.operation.controller.web; | ||
|
||
import static org.sopt.makers.operation.common.ApiResponse.*; | ||
import static org.sopt.makers.operation.common.ResponseMessage.*; | ||
|
||
import java.net.URI; | ||
|
||
import org.sopt.makers.operation.common.ApiResponse; | ||
import org.sopt.makers.operation.dto.alarm.AlarmRequestDTO; | ||
import org.sopt.makers.operation.entity.Part; | ||
import org.sopt.makers.operation.entity.alarm.Status; | ||
import org.sopt.makers.operation.service.AlarmService; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
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.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import lombok.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/alarms") | ||
public class AlarmController { | ||
private final AlarmService alarmService; | ||
|
||
@ApiOperation("알림 생성") | ||
@PostMapping | ||
public ResponseEntity<ApiResponse> createAlarm(@RequestBody AlarmRequestDTO requestDTO) { | ||
val alarmId = alarmService.createAlarm(requestDTO); | ||
return ResponseEntity | ||
.created(getURI(alarmId)) | ||
.body(success(SUCCESS_CREATE_ALARM.getMessage(), alarmId)); | ||
} | ||
|
||
@ApiOperation("알림 리스트 조회") | ||
@GetMapping | ||
public ResponseEntity<ApiResponse> getAlarms( | ||
@RequestParam(required = false) Integer generation, | ||
@RequestParam(required = false) Part part, | ||
@RequestParam(required = false) Status status, | ||
Pageable pageable | ||
) { | ||
val response = alarmService.getAlarms(generation, part, status, pageable); | ||
return ResponseEntity.ok(success(SUCCESS_GET_ALARMS.getMessage(), response)); | ||
} | ||
|
||
@ApiOperation("알림 상세 조회") | ||
@GetMapping("/{alarmId}") | ||
public ResponseEntity<ApiResponse> getAlarm(@PathVariable Long alarmId) { | ||
val response = alarmService.getAlarm(alarmId); | ||
return ResponseEntity.ok(success(SUCCESS_GET_ALARM.getMessage(), response)); | ||
} | ||
|
||
@ApiOperation("알림 삭제") | ||
@DeleteMapping("/{alarmId}") | ||
public ResponseEntity<ApiResponse> deleteAlarm(@PathVariable Long alarmId) { | ||
alarmService.deleteAlarm(alarmId); | ||
return ResponseEntity.ok(success(SUCCESS_DELETE_ALARM.getMessage())); | ||
} | ||
|
||
private URI getURI(Long alarmId) { | ||
return ServletUriComponentsBuilder | ||
.fromCurrentRequest() | ||
.path("/{alarmId}") | ||
.buildAndExpand(alarmId) | ||
.toUri(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/org/sopt/makers/operation/dto/alarm/AlarmResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.sopt.makers.operation.dto.alarm; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record AlarmResponseDTO( | ||
String attribute, | ||
String part, | ||
Boolean isActive, | ||
String title, | ||
String content, | ||
String link, | ||
String createdAt, | ||
String sendAt | ||
) { | ||
public static AlarmResponseDTO of(Alarm alarm) { | ||
return AlarmResponseDTO.builder() | ||
.attribute(alarm.getAttribute().getName()) | ||
.part(alarm.getPart().getName()) | ||
.isActive(alarm.getIsActive()) | ||
.title(alarm.getTitle()) | ||
.content(alarm.getContent()) | ||
.link(alarm.getLink()) | ||
.createdAt(alarm.getCreatedDate().toString()) | ||
.sendAt(nonNull(alarm.getSendAt()) ? alarm.getSendAt().toString() : null) | ||
.build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/sopt/makers/operation/dto/alarm/AlarmsResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.sopt.makers.operation.dto.alarm; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import java.util.List; | ||
|
||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
|
||
import lombok.Builder; | ||
|
||
public record AlarmsResponseDTO( | ||
List<AlarmVO> alarms | ||
) { | ||
public static AlarmsResponseDTO of(List<Alarm> alarms) { | ||
return new AlarmsResponseDTO(alarms.stream().map(AlarmVO::of).toList()); | ||
} | ||
|
||
@Builder | ||
record AlarmVO( | ||
Long alarmId, | ||
String part, | ||
String attribute, | ||
String title, | ||
String content, | ||
String sendAt, | ||
String status | ||
) { | ||
static AlarmVO of(Alarm alarm) { | ||
return AlarmVO.builder() | ||
.alarmId(alarm.getId()) | ||
.part(nonNull(alarm.getPart()) ? alarm.getPart().getName() : null) | ||
.attribute(alarm.getAttribute().getName()) | ||
.title(alarm.getTitle()) | ||
.content(alarm.getContent()) | ||
.sendAt(nonNull(alarm.getSendAt()) ? alarm.getSendAt().toString() : null) | ||
.status(alarm.getStatus().getName()) | ||
.build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/org/sopt/makers/operation/repository/alarm/AlarmCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.sopt.makers.operation.repository.alarm; | ||
|
||
import java.util.List; | ||
|
||
import org.sopt.makers.operation.entity.Part; | ||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
import org.sopt.makers.operation.entity.alarm.Status; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface AlarmCustomRepository { | ||
List<Alarm> getAlarms(Integer generation, Part part, Status status, Pageable pageable); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/sopt/makers/operation/repository/alarm/AlarmRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.sopt.makers.operation.repository.alarm; | ||
|
||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AlarmRepository extends JpaRepository<Alarm, Long>, AlarmCustomRepository { | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/sopt/makers/operation/repository/alarm/AlarmRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.sopt.makers.operation.repository.alarm; | ||
|
||
import static java.util.Objects.*; | ||
import static org.sopt.makers.operation.entity.Part.*; | ||
import static org.sopt.makers.operation.entity.alarm.QAlarm.*; | ||
|
||
import java.util.List; | ||
|
||
import org.sopt.makers.operation.entity.Part; | ||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
import org.sopt.makers.operation.entity.alarm.Status; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class AlarmRepositoryImpl implements AlarmCustomRepository { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<Alarm> getAlarms(Integer generation, Part part, Status status, Pageable pageable) { | ||
return queryFactory | ||
.selectFrom(alarm) | ||
.where( | ||
generationEq(generation), | ||
partEq(part), | ||
statusEq(status) | ||
) | ||
.orderBy(alarm.createdDate.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression generationEq(Integer generation) { | ||
return nonNull(generation) ? alarm.generation.eq(generation) : null; | ||
} | ||
|
||
private BooleanExpression partEq(Part part) { | ||
return (nonNull(part) && !part.equals(ALL)) ? alarm.part.eq(part) : null; | ||
} | ||
|
||
private BooleanExpression statusEq(Status status) { | ||
return nonNull(status) ? alarm.status.eq(status) : null; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/sopt/makers/operation/service/AlarmService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.sopt.makers.operation.service; | ||
|
||
import org.sopt.makers.operation.dto.alarm.AlarmRequestDTO; | ||
import org.sopt.makers.operation.dto.alarm.AlarmResponseDTO; | ||
import org.sopt.makers.operation.dto.alarm.AlarmsResponseDTO; | ||
import org.sopt.makers.operation.entity.Part; | ||
import org.sopt.makers.operation.entity.alarm.Status; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface AlarmService { | ||
Long createAlarm(AlarmRequestDTO requestDTO); | ||
AlarmsResponseDTO getAlarms(Integer generation, Part part, Status status, Pageable pageable); | ||
AlarmResponseDTO getAlarm(Long alarmId); | ||
void deleteAlarm(Long alarmId); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/org/sopt/makers/operation/service/AlarmServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.sopt.makers.operation.service; | ||
|
||
import static org.sopt.makers.operation.common.ExceptionMessage.*; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
|
||
import org.sopt.makers.operation.dto.alarm.AlarmRequestDTO; | ||
import org.sopt.makers.operation.dto.alarm.AlarmResponseDTO; | ||
import org.sopt.makers.operation.dto.alarm.AlarmsResponseDTO; | ||
import org.sopt.makers.operation.entity.Part; | ||
import org.sopt.makers.operation.entity.alarm.Alarm; | ||
import org.sopt.makers.operation.entity.alarm.Status; | ||
import org.sopt.makers.operation.repository.alarm.AlarmRepository; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.*; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AlarmServiceImpl implements AlarmService { | ||
private final AlarmRepository alarmRepository; | ||
|
||
@Override | ||
@Transactional | ||
public Long createAlarm(AlarmRequestDTO requestDTO) { | ||
val alarmEntity = requestDTO.toEntity(); | ||
val savedAlarm = alarmRepository.save(alarmEntity); | ||
return savedAlarm.getId(); | ||
} | ||
|
||
@Override | ||
public AlarmsResponseDTO getAlarms(Integer generation, Part part, Status status, Pageable pageable) { | ||
val alarms = alarmRepository.getAlarms(generation, part, status, pageable); | ||
return AlarmsResponseDTO.of(alarms); | ||
} | ||
|
||
@Override | ||
public AlarmResponseDTO getAlarm(Long alarmId) { | ||
val alarm = findAlarm(alarmId); | ||
return AlarmResponseDTO.of(alarm); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public void deleteAlarm(Long alarmId) { | ||
val alarm = findAlarm(alarmId); | ||
alarmRepository.delete(alarm); | ||
} | ||
|
||
private Alarm findAlarm(Long alarmId) { | ||
return alarmRepository.findById(alarmId) | ||
.orElseThrow(() -> new EntityNotFoundException(INVALID_ALARM.getName())); | ||
} | ||
} |