Skip to content
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
Expand Up @@ -43,6 +43,9 @@ public class Event extends TimeStamped {
@Column(nullable = false)
private int limitPeople;

@Column(nullable = false)
private int availableSeats;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private EventStatus status;
Expand All @@ -55,13 +58,25 @@ private Event(Store store, String content, LocalDateTime openAt, LocalDateTime e
this.endAt = endAt;
this.eventTime = eventTime;
this.limitPeople = limitPeople;
this.availableSeats = limitPeople;
this.status = EventStatus.READY;
}

public void update(LocalDateTime openAt, LocalDateTime eventTime, Integer limitPeople) {
if (openAt != null) this.openAt = openAt;
if (eventTime != null) this.eventTime = eventTime;
if (limitPeople != null) this.limitPeople = limitPeople;
if (limitPeople != null) {
this.limitPeople = limitPeople;
this.availableSeats = limitPeople;
}
}

public void decreaseAvailableSeats() {
if (this.availableSeats > 0) {
this.availableSeats--;
} else {
throw new HandledException(ErrorCode.EVENT_FULL);
}
}

public boolean isReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.Optional;

public interface EventRepository extends JpaRepository<Event, Long> {
boolean existsByStore_IdAndEventTime(Long storeId, LocalDateTime eventTime);
Page<Event> findByStatus(EventStatus status, Pageable pageable);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT e FROM Event e WHERE e.id = :id")
Optional<Event> findByIdForUpdate(Long id);
Optional<Event> findByIdForUpdate(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public EventJoinResponseDto joinEventWithDBLock(Long eventId, AuthUser authUser)
throw new HandledException(ErrorCode.EVENT_ALREADY_JOINED);
}

if (eventJoinRepository.countByEvent(event) >= event.getLimitPeople()) {
throw new HandledException(ErrorCode.EVENT_FULL);
}
event.decreaseAvailableSeats();
eventRepository.save(event);

EventJoin eventJoin = EventJoin.builder()
.user(user)
Expand Down