Skip to content

Commit b20d02c

Browse files
authored
Merge pull request #289 from spring-team-7/feat/event
[refactor] ์ด๋ฒคํŠธ ์ฐธ์—ฌ DB ๋ฝ ๋กœ์ง ์ˆ˜์ •
2 parents 9f26d4c + 65ef94b commit b20d02c

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

โ€Žsrc/main/java/org/example/tablenow/domain/event/entity/Event.javaโ€Ž

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class Event extends TimeStamped {
4343
@Column(nullable = false)
4444
private int limitPeople;
4545

46+
@Column(nullable = false)
47+
private int availableSeats;
48+
4649
@Enumerated(EnumType.STRING)
4750
@Column(nullable = false)
4851
private EventStatus status;
@@ -55,13 +58,25 @@ private Event(Store store, String content, LocalDateTime openAt, LocalDateTime e
5558
this.endAt = endAt;
5659
this.eventTime = eventTime;
5760
this.limitPeople = limitPeople;
61+
this.availableSeats = limitPeople;
5862
this.status = EventStatus.READY;
5963
}
6064

6165
public void update(LocalDateTime openAt, LocalDateTime eventTime, Integer limitPeople) {
6266
if (openAt != null) this.openAt = openAt;
6367
if (eventTime != null) this.eventTime = eventTime;
64-
if (limitPeople != null) this.limitPeople = limitPeople;
68+
if (limitPeople != null) {
69+
this.limitPeople = limitPeople;
70+
this.availableSeats = limitPeople;
71+
}
72+
}
73+
74+
public void decreaseAvailableSeats() {
75+
if (this.availableSeats > 0) {
76+
this.availableSeats--;
77+
} else {
78+
throw new HandledException(ErrorCode.EVENT_FULL);
79+
}
6580
}
6681

6782
public boolean isReady() {

โ€Žsrc/main/java/org/example/tablenow/domain/event/repository/EventRepository.javaโ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
import org.springframework.data.jpa.repository.JpaRepository;
99
import org.springframework.data.jpa.repository.Lock;
1010
import org.springframework.data.jpa.repository.Query;
11+
import org.springframework.data.repository.query.Param;
1112

1213
import java.time.LocalDateTime;
1314
import java.util.Optional;
1415

1516
public interface EventRepository extends JpaRepository<Event, Long> {
1617
boolean existsByStore_IdAndEventTime(Long storeId, LocalDateTime eventTime);
1718
Page<Event> findByStatus(EventStatus status, Pageable pageable);
19+
1820
@Lock(LockModeType.PESSIMISTIC_WRITE)
1921
@Query("SELECT e FROM Event e WHERE e.id = :id")
20-
Optional<Event> findByIdForUpdate(Long id);
22+
Optional<Event> findByIdForUpdate(@Param("id") Long id);
2123
}

โ€Žsrc/main/java/org/example/tablenow/domain/event/service/EventJoinService.javaโ€Ž

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ public EventJoinResponseDto joinEventWithDBLock(Long eventId, AuthUser authUser)
6262
throw new HandledException(ErrorCode.EVENT_ALREADY_JOINED);
6363
}
6464

65-
if (eventJoinRepository.countByEvent(event) >= event.getLimitPeople()) {
66-
throw new HandledException(ErrorCode.EVENT_FULL);
67-
}
65+
event.decreaseAvailableSeats();
66+
eventRepository.save(event);
6867

6968
EventJoin eventJoin = EventJoin.builder()
7069
.user(user)

0 commit comments

Comments
ย (0)