Skip to content

Commit

Permalink
Merge pull request #78 from Team-MindWay/76-admin-notice-add-api
Browse files Browse the repository at this point in the history
🔀 :: 어드민 공지 등록 api
  • Loading branch information
Umjiseung authored Apr 17, 2024
2 parents 055d6d4 + e3193c3 commit 5dcc691
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mindway.server.v2.domain.notice.converter;


import com.mindway.server.v2.domain.notice.entity.Notice;
import com.mindway.server.v2.domain.notice.presentation.dto.request.NoticeAddRequestDto;
import com.mindway.server.v2.domain.user.entity.User;

public interface NoticeConverter {
Notice toEntity(NoticeAddRequestDto noticeAddRequestDto, User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mindway.server.v2.domain.notice.converter.impl;

import com.mindway.server.v2.domain.notice.converter.NoticeConverter;
import com.mindway.server.v2.domain.notice.entity.Notice;
import com.mindway.server.v2.domain.notice.presentation.dto.request.NoticeAddRequestDto;
import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.stereotype.Component;

@Component
public class NoticeConverterImpl implements NoticeConverter {

public Notice toEntity(NoticeAddRequestDto noticeAddRequestDto, User user) {
return Notice.builder()
.title(noticeAddRequestDto.getTitle())
.content(noticeAddRequestDto.getContent())
.user(user)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mindway.server.v2.domain.notice.entity;

import com.mindway.server.v2.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Builder
public class Notice {

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

private String title;

private String content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mindway.server.v2.domain.notice.exception;

import com.mindway.server.v2.global.exception.ErrorCode;
import com.mindway.server.v2.global.exception.MindWayException;

public class NotAccessStudentException extends MindWayException {
public NotAccessStudentException() {
super(ErrorCode.NOT_ACCESS_STUDENT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mindway.server.v2.domain.notice.presentation;

import com.mindway.server.v2.domain.notice.presentation.dto.request.NoticeAddRequestDto;
import com.mindway.server.v2.domain.notice.service.NoticeAddService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/notice")
public class NoticeController {

private final NoticeAddService noticeAddService;

@PostMapping
public ResponseEntity<Void> addNotice(@Valid @RequestBody NoticeAddRequestDto noticeAddRequestDto) {
noticeAddService.execute(noticeAddRequestDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mindway.server.v2.domain.notice.presentation.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class NoticeAddRequestDto {

@NotNull
private String title;
@NotNull
private String content;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mindway.server.v2.domain.notice.repository;


import com.mindway.server.v2.domain.notice.entity.Notice;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NoticeRepository extends JpaRepository<Notice, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mindway.server.v2.domain.notice.service;

import com.mindway.server.v2.domain.notice.presentation.dto.request.NoticeAddRequestDto;

public interface NoticeAddService {
void execute(NoticeAddRequestDto noticeAddRequestDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mindway.server.v2.domain.notice.service.impl;

import com.mindway.server.v2.domain.notice.converter.NoticeConverter;
import com.mindway.server.v2.domain.notice.entity.Notice;
import com.mindway.server.v2.domain.notice.exception.NotAccessStudentException;
import com.mindway.server.v2.domain.notice.presentation.dto.request.NoticeAddRequestDto;
import com.mindway.server.v2.domain.notice.repository.NoticeRepository;
import com.mindway.server.v2.domain.notice.service.NoticeAddService;
import com.mindway.server.v2.domain.user.entity.Authority;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.util.UserUtil;
import com.mindway.server.v2.global.annotation.ServiceWithTransaction;
import lombok.RequiredArgsConstructor;

@ServiceWithTransaction
@RequiredArgsConstructor
public class NoticeAddServiceImpl implements NoticeAddService {

private final NoticeConverter noticeConverter;
private final NoticeRepository noticeRepository;
private final UserUtil userUtil;

public void execute(NoticeAddRequestDto noticeAddRequestDto) {
User user = userUtil.getCurrentUser();

if (user.getAuthority() == Authority.ROLE_STUDENT)
throw new NotAccessStudentException();

Notice notice = noticeConverter.toEntity(noticeAddRequestDto, user);

noticeRepository.save(notice);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mindway.server.v2.global.security.config;

import com.mindway.server.v2.domain.user.entity.Authority;
import com.mindway.server.v2.global.security.filter.JwtFilter;
import com.mindway.server.v2.global.security.handler.JwtAccessDeniedHandler;
import com.mindway.server.v2.global.security.handler.JwtAuthenticationEntryPoint;
Expand Down Expand Up @@ -63,6 +64,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(HttpMethod.POST, "/api/v2/goal").authenticated()
.requestMatchers(HttpMethod.GET, "/api/v2/goal").authenticated()

// notice
.requestMatchers(HttpMethod.POST, "/api/v2/notice").hasAnyAuthority(Authority.ROLE_TEACHER.name(), Authority.ROLE_HELPER.name())

.anyRequest().authenticated()
)

Expand Down

0 comments on commit 5dcc691

Please sign in to comment.