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

세미나 출석체크 구현 및 운영서버 에러 메세지 수정 #135

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.gdsc_knu.official_homepage.controller.admin;

import com.gdsc_knu.official_homepage.dto.member.MemberResponse;
import com.gdsc_knu.official_homepage.entity.enumeration.Track;
import com.gdsc_knu.official_homepage.service.discord.DiscordClient;
import com.gdsc_knu.official_homepage.service.admin.AdminMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class AdminMemberCheckController {
private final AdminMemberService adminMemberService;
private final DiscordClient discordClient;

@GetMapping("members")
Copy link
Contributor

Choose a reason for hiding this comment

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

adminController 니깐 admin/members가 더 좋지 않을까요? 아래 api들도 api/admin/...이 좋아보입니다

Copy link
Member Author

Choose a reason for hiding this comment

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

타임리프 바로 띄워서 사용하는거라 로그인 없이 쓸려고 admin prefix 안 붙였습니다
그럼 로그인 페이지까지 만들어야해서 ...

public String getHtml(Model model) {
Copy link
Contributor

Choose a reason for hiding this comment

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

admin 페이지니깐 @TokenMember와 어드민 권한 검증이 필요해보입니다

Copy link
Member Author

Choose a reason for hiding this comment

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

위와 같은 이유입니다

Copy link
Contributor

Choose a reason for hiding this comment

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

넵 알겠습니다! 고생하셨습니다~

model.addAttribute("tracks", Track.values());
return "members";
}

@GetMapping("api/member/check")
public String getMemberByTrack(@RequestParam Track track, Model model) {
List<MemberResponse.WithTrack> response = adminMemberService.getMembersByTrack(track);
model.addAttribute("tracks", Track.values());
model.addAttribute("defaultTrack", track);
model.addAttribute("members", response);
return "members";
}

@PostMapping("api/member/check/attendance")
public ResponseEntity<String> check(@RequestParam List<String> memberNames,
@RequestParam Track track) {
discordClient.sendAttendance(memberNames, track.name());
return ResponseEntity.ok().body("출첵 완료!");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.gdsc_knu.official_homepage.dto;

import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDate;
import java.util.Base64;
import java.util.List;

@Getter
Expand All @@ -30,4 +36,75 @@ public DiscordMessage(String content, List<Embed> embeds) {
this.embeds = embeds;
this.tts = false;
}

public static DiscordMessage createAttendanceMessage(List<String> names, String track) {
String nameList = "";
for (String name : names) {
nameList += name+" ";
}
LocalDate date = LocalDate.now();
DiscordMessage.Embed embed = DiscordMessage.Embed.builder()
.description(
date +"\n"+
track +"\n"+
nameList
)
.build();
return DiscordMessage.builder()
.content("## 세미나 불참자")
.embeds(List.of(embed))
.build();
}

public static DiscordMessage createMessage(Exception e, HttpStatus status, HttpServletRequest request) {
DiscordMessage.Embed embed = DiscordMessage.Embed.builder()
.description(
"❗**사용자 정보** \n" + getUserToken(request) +
"\n\n" +
"❗**요청 API** \n" + getRequestAPI(request) +
"\n\n" +
"❗**상태 코드** \n" + status +
"\n\n" +
"❗**오류 메세지** \n" + getStackTrace(e)
)
.build();

return DiscordMessage.builder()
.content("## ⚠오류 발생")
.embeds(List.of(embed))
.build();
}

private static String getStackTrace(Exception e) {
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString().substring(0,300);
}

private static String getRequestAPI(HttpServletRequest request) {
String fullPath = request.getMethod() + " " + request.getRequestURL();

String queryString = request.getQueryString();
return queryString != null
? fullPath + "?" + queryString
: fullPath;
}



private static String getUserToken(HttpServletRequest request) {
String token = request.getHeader("Authorization");

if (token == null) {
return "사용자 토큰이 없음";
}
try {
String[] split_string = token.split("\\.");
String base64EncodedBody = split_string[1];
String body = new String(Base64.getDecoder().decode(base64EncodedBody));
return body.substring(13);
} catch (Exception e) {
return "사용자 정보 가져오는데 실패함";
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,44 @@ public enum ErrorCode {
*/

// User
USER_NOT_FOUND(404,HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다"),
USER_NOT_FOUND(404,HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다","U404"),

// Auth
INVALID_PERMISSION(401,HttpStatus.UNAUTHORIZED, "인증되지 않은 사용자입니다."),
JWT_EXPIRED(401, HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다."),
JWT_NOT_FOUND(401, HttpStatus.UNAUTHORIZED, "엑세스 토큰이 존재하지 않습니다."),
RT_NOT_FOUND(401, HttpStatus.UNAUTHORIZED, "리프레시 토큰이 저장되어 있지 않습니다."),
JWT_INVALID(401, HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다."),
JWT_INCORRECT(401, HttpStatus.UNAUTHORIZED, "리프레시 토큰이 일치하지 않습니다."),
FORBIDDEN(403, HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),
INVALID_PERMISSION(401,HttpStatus.UNAUTHORIZED, "인증되지 않은 사용자입니다.","U401"),
JWT_EXPIRED(401, HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다.","ST401"),
JWT_NOT_FOUND(401, HttpStatus.UNAUTHORIZED, "엑세스 토큰이 존재하지 않습니다.","AT404"),
RT_NOT_FOUND(401, HttpStatus.UNAUTHORIZED, "리프레시 토큰이 저장되어 있지 않습니다.","RT404"),
JWT_INVALID(401, HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다.","ST404"),
JWT_INCORRECT(401, HttpStatus.UNAUTHORIZED, "리프레시 토큰이 일치하지 않습니다.","RT404"),
FORBIDDEN(403, HttpStatus.FORBIDDEN, "접근 권한이 없습니다.","U403"),

// Application
APPLICATION_NOT_FOUND(404,HttpStatus.NOT_FOUND, "지원서를 찾을 수 없습니다"),
CONFLICT(409, HttpStatus.CONFLICT, "이미 최종 제출된 지원서 입니다."),
INVALID_APPLICATION_STATE(400, HttpStatus.BAD_REQUEST,"지원서 상태가 유효하지 않습니다."),
APPLICATION_NOT_FOUND(404,HttpStatus.NOT_FOUND, "지원서를 찾을 수 없습니다","A404"),
CONFLICT(409, HttpStatus.CONFLICT, "이미 최종 제출된 지원서 입니다.","A409"),
INVALID_APPLICATION_STATE(400, HttpStatus.BAD_REQUEST,"지원서 상태가 유효하지 않습니다.","A400"),

// Team


INVALID_INPUT(400, HttpStatus.BAD_REQUEST,"잘못된 요청입니다."),
INVALID_INPUT(400, HttpStatus.BAD_REQUEST,"잘못된 요청입니다.","T400"),

// Post
POST_NOT_FOUND(404, HttpStatus.NOT_FOUND, "게시글을 찾을 수 없습니다."),
POST_FORBIDDEN(403, HttpStatus.FORBIDDEN, "게시글을 수정할 수 있는 권한이 없습니다."),
POST_ALREADY_LIKED(409, HttpStatus.CONFLICT, "이미 좋아요를 누른 게시글입니다."),
POST_NOT_LIKED(404, HttpStatus.NOT_FOUND, "좋아요를 누르지 않은 게시글입니다."),
POST_NOT_FOUND(404, HttpStatus.NOT_FOUND, "게시글을 찾을 수 없습니다.","P404"),
POST_FORBIDDEN(403, HttpStatus.FORBIDDEN, "게시글을 수정할 수 있는 권한이 없습니다.","P403"),
POST_ALREADY_LIKED(409, HttpStatus.CONFLICT, "이미 좋아요를 누른 게시글입니다.","PL409"),
POST_NOT_LIKED(404, HttpStatus.NOT_FOUND, "좋아요를 누르지 않은 게시글입니다.","PL404"),

// Comment
COMMENT_NOT_FOUND(404, HttpStatus.NOT_FOUND, "댓글을 찾을 수 없습니다."),
COMMENT_FORBIDDEN(403, HttpStatus.FORBIDDEN, "댓글을 수정할 수 있는 권한이 없습니다."),
INVALID_COMMENT(400, HttpStatus.BAD_REQUEST, "상위 댓글에만 답글을 남길 수 있습니다."),
COMMENT_NOT_FOUND(404, HttpStatus.NOT_FOUND, "댓글을 찾을 수 없습니다.","C404"),
COMMENT_FORBIDDEN(403, HttpStatus.FORBIDDEN, "댓글을 수정할 수 있는 권한이 없습니다.","C403"),
INVALID_COMMENT(400, HttpStatus.BAD_REQUEST, "상위 댓글에만 답글을 남길 수 있습니다.","C400"),

// Server error
FAILED_UPLOAD(500, HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다."),
FAILED_SEND_MAIL(500, HttpStatus.INTERNAL_SERVER_ERROR, "메일 전송에 실패했습니다.");
FAILED_UPLOAD(500, HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다.","F500"),
FAILED_SEND_MAIL(500, HttpStatus.INTERNAL_SERVER_ERROR, "메일 전송에 실패했습니다.","M500");

private final int status;
private final HttpStatus error;
private final String message;
private final String code;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

@Getter
public class ExceptionDto {
private int code;
private String message;
private HttpStatus data;
private final int code;
private final String message;
private final HttpStatus data;

public ExceptionDto(ErrorCode error) {
this.code = error.getStatus();
this.message = error.getMessage();
this.message = error.getCode();
this.data = error.getError();
}

Expand Down
Loading
Loading