Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ :: ์–ด๋“œ๋ฏผ ๊ณต์ง€ ๋“ฑ๋ก api #78

Merged
merged 9 commits into from
Apr 17, 2024
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();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์„ ์ƒ๋‹˜๋งŒ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์•ผํ• ๊ฑฐ๊ฐ™์•„์š”

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0464d2c
์ˆ˜์ •ํ–ˆ์–ด์šฉ

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
Loading