Skip to content

Commit 2a7670b

Browse files
authored
Use timestamp instead of boolean (#23)
* Use timestamp instead of boolean to indicate whether a user is subscribed or not * Add migration file
1 parent 9487d41 commit 2a7670b

File tree

7 files changed

+65
-59
lines changed

7 files changed

+65
-59
lines changed

src/main/java/org/openpodcastapi/opa/subscription/SubscriptionDTO.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openpodcastapi.opa.subscription;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import jakarta.annotation.Nullable;
45
import jakarta.validation.constraints.NotNull;
56
import lombok.NonNull;
67
import org.hibernate.validator.constraints.URL;
@@ -23,17 +24,17 @@ public record SubscriptionCreateDTO(
2324

2425
/// A DTO representing a user's subscription to a given feed
2526
///
26-
/// @param uuid the feed UUID
27-
/// @param feedUrl the feed URL
28-
/// @param createdAt the date at which the subscription link was created
29-
/// @param updatedAt the date at which the subscription link was last updated
30-
/// @param isSubscribed whether the user is currently subscribed to the feed
27+
/// @param uuid the feed UUID
28+
/// @param feedUrl the feed URL
29+
/// @param createdAt the date at which the subscription link was created
30+
/// @param updatedAt the date at which the subscription link was last updated
31+
/// @param unsubscribedAt the date at which the user unsubscribed from the feed
3132
public record UserSubscriptionDTO(
3233
@JsonProperty(required = true) @UUID java.util.UUID uuid,
3334
@JsonProperty(required = true) @URL String feedUrl,
3435
@JsonProperty(required = true) Instant createdAt,
3536
@JsonProperty(required = true) Instant updatedAt,
36-
@JsonProperty(required = true) Boolean isSubscribed
37+
@JsonProperty @Nullable Instant unsubscribedAt
3738
) {
3839
}
3940

src/main/java/org/openpodcastapi/opa/subscription/SubscriptionService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.stereotype.Service;
1111
import org.springframework.transaction.annotation.Transactional;
1212

13+
import java.time.Instant;
1314
import java.util.ArrayList;
1415
import java.util.List;
1516
import java.util.UUID;
@@ -73,7 +74,7 @@ public SubscriptionDTO.UserSubscriptionDTO getUserSubscriptionBySubscriptionUuid
7374
@Transactional(readOnly = true)
7475
public Page<SubscriptionDTO.@NonNull UserSubscriptionDTO> getAllActiveSubscriptionsForUser(Long userId, Pageable pageable) {
7576
log.debug("Fetching all active subscriptions for {}", userId);
76-
return userSubscriptionRepository.findAllByUserIdAndIsSubscribedTrue(userId, pageable).map(userSubscriptionMapper::toDto);
77+
return userSubscriptionRepository.findAllByUserIdAndUnsubscribedAtNotEmpty(userId, pageable).map(userSubscriptionMapper::toDto);
7778
}
7879

7980
/// Persists a new user subscription to the database
@@ -91,13 +92,13 @@ protected SubscriptionDTO.UserSubscriptionDTO persistUserSubscription(Subscripti
9192
final var newSubscription = userSubscriptionRepository.findByUserIdAndSubscriptionUuid(userId, subscriptionEntity.getUuid()).orElseGet(() -> {
9293
log.debug("Creating new subscription for user {} and subscription {}", userId, subscriptionEntity.getUuid());
9394
final var createdSubscriptionEntity = new UserSubscriptionEntity();
94-
createdSubscriptionEntity.setIsSubscribed(true);
95+
createdSubscriptionEntity.setUnsubscribedAt(null);
9596
createdSubscriptionEntity.setUser(userEntity);
9697
createdSubscriptionEntity.setSubscription(subscriptionEntity);
9798
return userSubscriptionRepository.save(createdSubscriptionEntity);
9899
});
99100

100-
newSubscription.setIsSubscribed(true);
101+
newSubscription.setUnsubscribedAt(null);
101102
return userSubscriptionMapper.toDto(userSubscriptionRepository.save(newSubscription));
102103
}
103104

@@ -143,7 +144,7 @@ public SubscriptionDTO.UserSubscriptionDTO unsubscribeUserFromFeed(UUID feedUUID
143144
final var userSubscriptionEntity = userSubscriptionRepository.findByUserIdAndSubscriptionUuid(userId, feedUUID)
144145
.orElseThrow(() -> new EntityNotFoundException("no subscription found"));
145146

146-
userSubscriptionEntity.setIsSubscribed(false);
147+
userSubscriptionEntity.setUnsubscribedAt(Instant.now());
147148
return userSubscriptionMapper.toDto(userSubscriptionRepository.save(userSubscriptionEntity));
148149
}
149150
}

src/main/java/org/openpodcastapi/opa/subscription/UserSubscriptionEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public class UserSubscriptionEntity {
3131
@JoinColumn(name = "subscription_id")
3232
private SubscriptionEntity subscription;
3333

34-
@Column(columnDefinition = "boolean default true")
35-
private Boolean isSubscribed;
36-
3734
@Column(nullable = false, updatable = false)
3835
private Instant createdAt;
3936

4037
@Column(nullable = false)
4138
private Instant updatedAt;
4239

40+
@Column
41+
private Instant unsubscribedAt;
42+
4343
@PrePersist
4444
public void prePersist() {
4545
this.setUuid(UUID.randomUUID());

src/main/java/org/openpodcastapi/opa/subscription/UserSubscriptionRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public interface UserSubscriptionRepository extends JpaRepository<@NonNull UserS
1515

1616
Page<@NonNull UserSubscriptionEntity> findAllByUserId(Long userId, Pageable pageable);
1717

18-
Page<@NonNull UserSubscriptionEntity> findAllByUserIdAndIsSubscribedTrue(Long userId, Pageable pageable);
18+
Page<@NonNull UserSubscriptionEntity> findAllByUserIdAndUnsubscribedAtNotEmpty(Long userId, Pageable pageable);
1919
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE user_subscription
2+
ADD unsubscribed_at TIMESTAMP WITHOUT TIME ZONE;
3+
4+
ALTER TABLE user_subscription
5+
DROP COLUMN is_subscribed;

0 commit comments

Comments
 (0)