Skip to content

Commit

Permalink
Merge pull request #431 from Namo-log/feature/429
Browse files Browse the repository at this point in the history
[Bug Fix/429] participant의 palette column 삭제 및 anonymous로 이동
  • Loading branch information
joowojr authored Jan 12, 2025
2 parents ec6b014 + f98d1c2 commit 67ecc8f
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.stream.Collectors;

import com.namo.spring.application.external.global.config.properties.WebUrlConfig;
import com.namo.spring.db.mysql.domains.category.service.PaletteService;
import com.namo.spring.db.mysql.domains.user.entity.Member;
import org.springframework.stereotype.Service;

import com.namo.spring.application.external.api.guest.dto.GuestParticipantRequest;
Expand Down Expand Up @@ -38,21 +40,22 @@ public class GuestManageService {
private final AnonymousService anonymousService;
private final ParticipantService participantService;
private final ParticipantMaker participantMaker;
private final PaletteService paletteService;
private final TagGenerator tagGenerator;
private final WebUrlConfig webUrlConfig;

public Anonymous createAnonymous(GuestParticipantRequest.PostGuestParticipantDto dto, Schedule schedule,
String code, String tag) {
Anonymous anonymous = Anonymous.of(null, null, tag, dto.getNickname(), dto.getPassword(), code);
public Anonymous createAnonymous(GuestParticipantRequest.PostGuestParticipantDto dto, String code, String tag, Palette palette) {
Anonymous anonymous = Anonymous.of(null, null, tag, dto.getNickname(), dto.getPassword(), code, palette);
return anonymousService.createAnonymous(anonymous);
}

private Participant createGuest(GuestParticipantRequest.PostGuestParticipantDto dto, Schedule schedule,
String code) {
String tag = tagGenerator.generateTag(dto.getNickname());
Anonymous anonymous = createAnonymous(dto, schedule, code, tag);
Long paletteId = selectPaletteColorId(schedule.getId());
return participantMaker.makeGuestParticipant(schedule, anonymous, paletteId);
Palette palette = paletteService.readPalette(paletteId).orElseThrow(()-> new PaletteException(ErrorStatus.NOT_FOUND_COLOR));
Anonymous anonymous = createAnonymous(dto, code, tag, palette);
return participantMaker.makeGuestParticipant(schedule, anonymous);
}

public Participant getAnonymousParticipant(Long anonymousId, Long scheduleId) {
Expand All @@ -71,7 +74,7 @@ public Anonymous getAnonymousByTagAndNickname(String tag, String nickname) {
private Long selectPaletteColorId(Long scheduleId) {
List<Long> participantsColors = participantService.readParticipantsByScheduleIdAndScheduleType(scheduleId,
ScheduleType.MEETING)
.stream().map(Participant::getPalette).map(Palette::getId).collect(Collectors.toList());
.stream().map(Participant::getMember).map(Member::getPalette).map(Palette::getId).collect(Collectors.toList());
return Arrays.stream(PALETTE_IDS)
.filter((color) -> !participantsColors.contains(color))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import lombok.RequiredArgsConstructor;

@Tag(name = "11. 포인트 - 관리자", description = "포인트 관련 관리자 API")
@Tag(name = "12. 포인트 - 관리자", description = "포인트 관련 관리자 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/admin/points/transactions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.RequiredArgsConstructor;

@Tag(name = "11. 포인트 - 유저", description = "포인트 관련 API")
@Tag(name = "12. 포인트 - 유저", description = "포인트 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/users/points")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static MeetingScheduleResponse.UserParticipantDetailDto toUserParticipan
.isGuest(participant.getUser() instanceof Anonymous)
.tag(participant.getUser().getTag())
.nickname(participant.getUser().getNickname())
.colorId(participant.getPalette() != null ? participant.getPalette().getId() : null)
.colorId(participant.getPalette().getId())
.isOwner(getParticipantIsOwner(participant.getIsOwner()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ public class ParticipantMaker {
private final CategoryService categoryService;
private final ParticipantService participantService;

public void makeScheduleOwner(Schedule schedule, Member member, Long categoryId, Long paletteId) {
public void makeScheduleOwner(Schedule schedule, Member member, Long categoryId) {
Category category;
if (categoryId != null) {
category = categoryService.readCategoryByMemberAndId(categoryId, member);
} else
category = categoryService.readMeetingCategoryByMember(member);
Palette palette = paletteId != null ? paletteService.getPalette(paletteId) : null;

Participant participant;
if(schedule.getIsMeetingSchedule()){
participant = Participant.of(ParticipantRole.OWNER.getValue(), member, schedule, category, palette, schedule.getTitle(), schedule.getImageUrl());
} else participant = Participant.of(ParticipantRole.OWNER.getValue(), member, schedule, category, palette, null, null);
participant = Participant.of(ParticipantRole.OWNER.getValue(), member, schedule, category, schedule.getTitle(), schedule.getImageUrl());
} else participant = Participant.of(ParticipantRole.OWNER.getValue(), member, schedule, category,null, null);
participantService.createParticipant(participant);
}

Expand All @@ -47,16 +46,15 @@ public void makeMeetingScheduleParticipants(Schedule schedule, List<Member> memb
.map(member -> {
Category category = categoryService.readMeetingCategoryByMember(member);
return Participant.of(ParticipantRole.NON_OWNER.getValue(), member, schedule,
category, member.getPalette(), schedule.getTitle(), schedule.getImageUrl());
category, schedule.getTitle(), schedule.getImageUrl());
})
.collect(Collectors.toList());
participantService.createParticipants(participants);
}

public Participant makeGuestParticipant(Schedule schedule, Anonymous anonymous, Long paletteId) {
Palette palette = paletteService.getPalette(paletteId);
public Participant makeGuestParticipant(Schedule schedule, Anonymous anonymous) {
Participant participant = Participant.of(ParticipantRole.NON_OWNER.getValue(), anonymous, schedule,
null, palette, schedule.getTitle(), schedule.getImageUrl());
null, schedule.getTitle(), schedule.getImageUrl());
Participant savedParticipant = participantService.createParticipant(participant);
schedule.setGuestParticipantsInfo(anonymous.getNickname());
return savedParticipant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class ParticipantManageService {
private final FriendshipService friendshipService;
private final ParticipantService participantService;

public void createScheduleOwner(Member member, Schedule schedule, Long categoryId, Long paletteId) {
participantMaker.makeScheduleOwner(schedule, member, categoryId, paletteId);
public void createScheduleOwner(Member member, Schedule schedule, Long categoryId) {
participantMaker.makeScheduleOwner(schedule, member, categoryId);
}

public void createMeetingParticipants(Member owner, Schedule schedule, List<Long> memberIds){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public Schedule getMeetingSchedule(Long scheduleId) {
public Schedule createPersonalSchedule(PersonalScheduleRequest.PostPersonalScheduleDto request, Member member) {
Period period = getValidatedPeriod(request.getPeriod().getStartDate(), request.getPeriod().getEndDate());
Schedule schedule = scheduleMaker.createPersonalSchedule(request, period, member.getNickname());
participantManageService.createScheduleOwner(member, schedule, request.getCategoryId(), null);
participantManageService.createScheduleOwner(member, schedule, request.getCategoryId());
return schedule;
}

public Schedule createMeetingSchedule(MeetingScheduleRequest.PostMeetingScheduleDto request, Member owner) {
Period period = getValidatedPeriod(request.getPeriod().getStartDate(), request.getPeriod().getEndDate());
Schedule schedule = scheduleMaker.createMeetingSchedule(request, period, owner.getNickname());
participantManageService.createScheduleOwner(owner, schedule, null, owner.getPalette().getId());
participantManageService.createScheduleOwner(owner, schedule, null);
return schedule;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@RestController
@RequiredArgsConstructor
@Tag(name = "11. Profile", description = "유저 프로필 API")
@Tag(name = "11. Member Profile", description = "유저 프로필 API")
@RequestMapping("/api/v2/members")
public class MemberProfileController {
private final MemberProfileUsecase memberProfileUsecase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ public class Participant extends BaseTimeEntity {
@JoinColumn(name = "category_id", nullable = true)
private Category category;

@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "palette_id", nullable = true)
private Palette palette;

@JdbcTypeCode(SqlTypes.VARCHAR)
@Column(length = 50)
private String customTitle;
Expand All @@ -78,27 +74,23 @@ public class Participant extends BaseTimeEntity {
private Diary diary;

@Builder
public Participant(int isOwner, User user, Schedule schedule, Category category,
Palette palette, String customTitle, String customImage) {
public Participant(int isOwner, User user, Schedule schedule, Category category, String customTitle, String customImage) {
this.isOwner = Objects.requireNonNull(isOwner, "isOwner은 null일 수 없습니다.");
this.member = user instanceof Member ? (Member)user : null;
this.anonymous = user instanceof Anonymous ? (Anonymous)user : null;
this.schedule = Objects.requireNonNull(schedule, "schedule은 null일 수 없습니다.");
this.category = category;
this.palette = palette;
this.hasDiary = false;
this.customTitle = customTitle;
this.customImage =customImage;
}

public static Participant of(int isOwner, User user, Schedule schedule, Category category,
Palette palette, String customTitle, String customImage) {
public static Participant of(int isOwner, User user, Schedule schedule, Category category, String customTitle, String customImage) {
return Participant.builder()
.isOwner(isOwner)
.user(user)
.schedule(schedule)
.category(category)
.palette(palette)
.customTitle(customTitle)
.customImage(customImage)
.build();
Expand All @@ -113,6 +105,15 @@ public User getUser() {
return null;
}

public Palette getPalette() {
if(this.getUser() instanceof Member){
return this.member.getPalette();
}
else {
return this.anonymous.getPalette();
}
}

public void setIsOwner(ParticipantRole role) {
this.isOwner = role.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface ParticipantRepository extends JpaRepository<Participant, Long>

boolean existsByScheduleIdAndAnonymousId(Long scheduleId, Long anonymousId);

@Query("SELECT p FROM Participant p JOIN FETCH p.schedule s LEFT JOIN FETCH p.palette " +
@Query("SELECT p FROM Participant p JOIN FETCH p.schedule s " +
"WHERE s.id = :scheduleId AND s.scheduleType = :scheduleType ")
List<Participant> findParticipantsByScheduleIdAndStatusAndType(Long scheduleId, int scheduleType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import static com.namo.spring.db.mysql.domains.user.utils.UserValidationUtils.*;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import com.namo.spring.db.mysql.domains.category.entity.Palette;
import jakarta.persistence.*;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.JdbcTypeCode;
Expand Down Expand Up @@ -50,27 +46,33 @@ public class Anonymous extends BaseTimeEntity implements User {
@Embedded
private Password password;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "palette_id")
private Palette palette;

private String inviteCode;

@Builder
public Anonymous(String name, Boolean nameVisible, String tag,
String nickname, String password, String inviteCode) {
String nickname, String password, String inviteCode, Palette palette) {
this.name = name;
this.nameVisible = nameVisible;
this.tag = validateTag(tag);
this.nickname = validateNickname(nickname);
this.password = Password.encrypt(password);
this.palette = palette;
this.inviteCode = inviteCode;
}

public static Anonymous of(String name, Boolean nameVisible, String tag,
String nickname, String password, String inviteCode) {
String nickname, String password, String inviteCode, Palette palette) {
return Anonymous.builder()
.nickname(nickname)
.name(name)
.nameVisible(nameVisible)
.tag(tag)
.password(password)
.palette(palette)
.inviteCode(inviteCode)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- V1.1.8__Add_palette_column_to_anonymous_table

-- 1. anonymous에 palette_id 추가
ALTER TABLE anonymous
ADD COLUMN palette_id BIGINT,
ADD CONSTRAINT fk_anonymous_palette FOREIGN KEY (palette_id) REFERENCES palette (id);

-- 2. participant의 palette_id를 anonymous의 palette_id로 복사
UPDATE anonymous a
JOIN participant p ON a.id = p.anonymous_id
SET a.palette_id = p.palette_id
WHERE p.palette_id IS NOT NULL;

ALTER TABLE anonymous
MODIFY COLUMN palette_id BIGINT NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- V1.1.9__Drop_palette_column_from_participant_table

SET foreign_key_checks = 0;
ALTER TABLE participant DROP FOREIGN KEY FK7q1eb1aao98hr1nsa85agu6x5;
ALTER TABLE participant DROP COLUMN palette_id;
SET foreign_key_checks = 1;

0 comments on commit 67ecc8f

Please sign in to comment.