Skip to content

Commit

Permalink
✨ [Feature] AgendaProfile 상세조회 - 사진 및 업적 조회 기능 추가 #940 (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhames authored Aug 26, 2024
1 parent d62dce7 commit ef93835
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,29 @@
import gg.agenda.api.user.agendaprofile.controller.response.CurrentAttendAgendaListResDto;
import gg.agenda.api.user.agendaprofile.service.AgendaProfileFindService;
import gg.agenda.api.user.agendaprofile.service.AgendaProfileService;
import gg.agenda.api.user.agendaprofile.service.IntraProfileUtils;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile;
import gg.agenda.api.user.ticket.service.TicketService;
import gg.auth.UserDto;
import gg.auth.argumentresolver.Login;
import gg.data.agenda.AgendaProfile;
import gg.data.agenda.AgendaTeamProfile;
import gg.data.user.type.RoleType;
import gg.utils.dto.PageRequestDto;
import gg.utils.dto.PageResponseDto;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/agenda/profile")
public class AgendaProfileController {
private final AgendaProfileFindService agendaProfileFindService;
private final AgendaProfileService agendaProfileService;
private final TicketService ticketService;
private final IntraProfileUtils intraProfileUtils;

/**
* AgendaProfile 상세 조회 API
Expand All @@ -49,9 +57,12 @@ public class AgendaProfileController {
@GetMapping
public ResponseEntity<AgendaProfileDetailsResDto> myAgendaProfileDetails(
@Login @Parameter(hidden = true) UserDto user) {
AgendaProfileDetailsResDto agendaProfileDetails = agendaProfileFindService.detailsAgendaProfile(
user.getIntraId());
return ResponseEntity.status(HttpStatus.OK).body(agendaProfileDetails);
AgendaProfile profile = agendaProfileFindService.findAgendaProfileByIntraId(user.getIntraId());
int ticketCount = ticketService.findTicketList(profile).size();
IntraProfile intraProfile = intraProfileUtils.getIntraProfile();
AgendaProfileDetailsResDto agendaProfileDetails = AgendaProfileDetailsResDto.toDto(
profile, ticketCount, intraProfile);
return ResponseEntity.ok(agendaProfileDetails);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gg.agenda.api.user.agendaprofile.controller.response;

import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile;
import gg.data.agenda.AgendaProfile;
import gg.data.agenda.type.Coalition;
import gg.data.agenda.type.Location;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -16,13 +18,29 @@ public class AgendaProfileDetailsResDto {
private Coalition userCoalition;
private Location userLocation;
private int ticketCount;
private IntraProfile intraProfile;

public AgendaProfileDetailsResDto(String intraId, AgendaProfile entity, int ticketCount) {
this.userIntraId = intraId;
this.userContent = entity.getContent();
this.userGithub = entity.getGithubUrl();
this.userCoalition = entity.getCoalition();
this.userLocation = entity.getLocation();
@Builder
public AgendaProfileDetailsResDto(String userIntraId, String userContent, String userGithub,
Coalition userCoalition, Location userLocation, int ticketCount, IntraProfile intraProfile) {
this.userIntraId = userIntraId;
this.userContent = userContent;
this.userGithub = userGithub;
this.userCoalition = userCoalition;
this.userLocation = userLocation;
this.ticketCount = ticketCount;
this.intraProfile = intraProfile;
}

public static AgendaProfileDetailsResDto toDto(AgendaProfile profile, int ticketCount, IntraProfile intraProfile) {
return AgendaProfileDetailsResDto.builder()
.userIntraId(profile.getIntraId())
.userContent(profile.getContent())
.userGithub(profile.getGithubUrl())
.userCoalition(profile.getCoalition())
.userLocation(profile.getLocation())
.ticketCount(ticketCount)
.intraProfile(intraProfile)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import gg.agenda.api.user.agendaprofile.controller.response.AgendaProfileDetailsResDto;
import gg.agenda.api.user.agendaprofile.controller.response.AttendedAgendaListResDto;
import gg.agenda.api.user.agendaprofile.controller.response.CurrentAttendAgendaListResDto;
import gg.data.agenda.Agenda;
import gg.data.agenda.AgendaProfile;
Expand All @@ -21,35 +19,23 @@
import gg.repo.agenda.AgendaProfileRepository;
import gg.repo.agenda.AgendaRepository;
import gg.repo.agenda.AgendaTeamProfileRepository;
import gg.repo.agenda.AgendaTeamRepository;
import gg.repo.agenda.TicketRepository;
import gg.repo.user.UserRepository;
import gg.utils.exception.custom.NotExistException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class AgendaProfileFindService {

private final AgendaProfileRepository agendaProfileRepository;
private final TicketRepository ticketRepository;
private final AgendaTeamProfileRepository agendaTeamProfileRepository;
private final AgendaRepository agendaRepository;

/**
* AgendaProfile 상세 정보를 조회하는 메서드
* @param intraId 로그인한 유저의 id
* @return AgendaProfileDetailsResDto 객체
*/
@Transactional(readOnly = true)
public AgendaProfileDetailsResDto detailsAgendaProfile(String intraId) {
AgendaProfile agendaProfile = agendaProfileRepository.findByIntraId(intraId)
public AgendaProfile findAgendaProfileByIntraId(String intraId) {
return agendaProfileRepository.findByIntraId(intraId)
.orElseThrow(() -> new NotExistException(AGENDA_PROFILE_NOT_FOUND));

int ticketCount = ticketRepository.findByAgendaProfileIdAndIsUsedFalseAndIsApprovedTrue(agendaProfile.getId())
.size();

return new AgendaProfileDetailsResDto(intraId, agendaProfile, ticketCount);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@ public void modifyAgendaProfile(Long userId, AgendaProfileChangeReqDto reqDto) {
agendaProfile.updateProfile(reqDto.getUserContent(), reqDto.getUserGithub());
agendaProfileRepository.save(agendaProfile);
}

@Transactional(readOnly = true)
public AgendaProfile getAgendaProfile(Long userId) {
return agendaProfileRepository.findByUserId(userId)
.orElseThrow(() -> new NotExistException(AGENDA_PROFILE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gg.agenda.api.user.agendaprofile.service;

import static gg.utils.exception.ErrorCode.*;

import java.util.List;
import java.util.Objects;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraAchievement;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraImage;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfileResponse;
import gg.auth.FortyTwoAuthUtil;
import gg.utils.exception.custom.AuthenticationException;
import gg.utils.external.ApiUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class IntraProfileUtils {
private static final String INTRA_PROFILE_URL = "https://api.intra.42.fr/v2/me";

private final FortyTwoAuthUtil fortyTwoAuthUtil;

private final ApiUtil apiUtil;

public IntraProfile getIntraProfile() {
IntraProfileResponse intraProfileResponse = requestIntraProfile();
intraProfileResponseValidation(intraProfileResponse);
IntraImage intraImage = intraProfileResponse.getImage();
List<IntraAchievement> intraAchievements = intraProfileResponse.getAchievements();
return new IntraProfile(intraImage.getLink(), intraAchievements);
}

private IntraProfileResponse requestIntraProfile() {
try {
String accessToken = fortyTwoAuthUtil.getAccessToken();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
return apiUtil.apiCall(INTRA_PROFILE_URL, IntraProfileResponse.class, headers, HttpMethod.GET);
} catch (Exception e) {
String accessToken = fortyTwoAuthUtil.refreshAccessToken();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
return apiUtil.apiCall(INTRA_PROFILE_URL, IntraProfileResponse.class, headers, HttpMethod.GET);
}
}

private void intraProfileResponseValidation(IntraProfileResponse intraProfileResponse) {
if (Objects.isNull(intraProfileResponse)) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
if (Objects.isNull(intraProfileResponse.getImage())) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
if (Objects.isNull(intraProfileResponse.getAchievements())) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
if (Objects.isNull(intraProfileResponse.getImage().getLink())) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gg.agenda.api.user.agendaprofile.service.intraprofile;

import java.net.URL;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraAchievement {

private static final String IMAGE_URL = "https://cdn.intra.42.fr";

private Long id;

private String name;

private String description;

private String tier;

private String kind;

private boolean visible;

private String image;

@JsonProperty("nbr_of_success")
private String nbrOfSuccess;

@JsonProperty("users_url")
private URL usersUrl;

@Builder
public IntraAchievement(Long id, String name, String description, String tier, String kind, boolean visible,
String image, String nbrOfSuccess, URL usersUrl) {
this.id = id;
this.name = name;
this.description = description;
this.tier = tier;
this.kind = kind;
this.visible = visible;
this.image = image;
this.nbrOfSuccess = nbrOfSuccess;
this.usersUrl = usersUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gg.agenda.api.user.agendaprofile.service.intraprofile;

import java.net.URL;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraImage {

private URL link;

private IntraImageVersion versions;

@Builder
public IntraImage(URL link, IntraImageVersion versions) {
this.link = link;
this.versions = versions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gg.agenda.api.user.agendaprofile.service.intraprofile;

import java.net.URL;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraImageVersion {

private URL large;

private URL medium;

private URL small;

private URL micro;

@Builder
public IntraImageVersion(URL large, URL medium, URL small, URL micro) {
this.large = large;
this.medium = medium;
this.small = small;
this.micro = micro;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gg.agenda.api.user.agendaprofile.service.intraprofile;

import java.net.URL;
import java.util.List;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraProfile {

private URL imageUrl;

private List<IntraAchievement> achievements;

@Builder
public IntraProfile(URL imageUrl, List<IntraAchievement> achievements) {
this.imageUrl = imageUrl;
this.achievements = achievements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gg.agenda.api.user.agendaprofile.service.intraprofile;

import java.util.List;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraProfileResponse {

IntraImage image;

List<IntraAchievement> achievements;

@Builder
public IntraProfileResponse(IntraImage image, List<IntraAchievement> achievements) {
this.image = image;
this.achievements = achievements;
}
}
Loading

0 comments on commit ef93835

Please sign in to comment.