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
@@ -1,6 +1,7 @@
package org.openpodcastapi.opa.subscription;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;
import org.hibernate.validator.constraints.URL;
Expand All @@ -23,17 +24,17 @@ public record SubscriptionCreateDTO(

/// A DTO representing a user's subscription to a given feed
///
/// @param uuid the feed UUID
/// @param feedUrl the feed URL
/// @param createdAt the date at which the subscription link was created
/// @param updatedAt the date at which the subscription link was last updated
/// @param isSubscribed whether the user is currently subscribed to the feed
/// @param uuid the feed UUID
/// @param feedUrl the feed URL
/// @param createdAt the date at which the subscription link was created
/// @param updatedAt the date at which the subscription link was last updated
/// @param unsubscribedAt the date at which the user unsubscribed from the feed
public record UserSubscriptionDTO(
@JsonProperty(required = true) @UUID java.util.UUID uuid,
@JsonProperty(required = true) @URL String feedUrl,
@JsonProperty(required = true) Instant createdAt,
@JsonProperty(required = true) Instant updatedAt,
@JsonProperty(required = true) Boolean isSubscribed
@JsonProperty @Nullable Instant unsubscribedAt
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -73,7 +74,7 @@ public SubscriptionDTO.UserSubscriptionDTO getUserSubscriptionBySubscriptionUuid
@Transactional(readOnly = true)
public Page<SubscriptionDTO.@NonNull UserSubscriptionDTO> getAllActiveSubscriptionsForUser(Long userId, Pageable pageable) {
log.debug("Fetching all active subscriptions for {}", userId);
return userSubscriptionRepository.findAllByUserIdAndIsSubscribedTrue(userId, pageable).map(userSubscriptionMapper::toDto);
return userSubscriptionRepository.findAllByUserIdAndUnsubscribedAtNotEmpty(userId, pageable).map(userSubscriptionMapper::toDto);
}

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

newSubscription.setIsSubscribed(true);
newSubscription.setUnsubscribedAt(null);
return userSubscriptionMapper.toDto(userSubscriptionRepository.save(newSubscription));
}

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

userSubscriptionEntity.setIsSubscribed(false);
userSubscriptionEntity.setUnsubscribedAt(Instant.now());
return userSubscriptionMapper.toDto(userSubscriptionRepository.save(userSubscriptionEntity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class UserSubscriptionEntity {
@JoinColumn(name = "subscription_id")
private SubscriptionEntity subscription;

@Column(columnDefinition = "boolean default true")
private Boolean isSubscribed;

@Column(nullable = false, updatable = false)
private Instant createdAt;

@Column(nullable = false)
private Instant updatedAt;

@Column
private Instant unsubscribedAt;

@PrePersist
public void prePersist() {
this.setUuid(UUID.randomUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface UserSubscriptionRepository extends JpaRepository<@NonNull UserS

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

Page<@NonNull UserSubscriptionEntity> findAllByUserIdAndIsSubscribedTrue(Long userId, Pageable pageable);
Page<@NonNull UserSubscriptionEntity> findAllByUserIdAndUnsubscribedAtNotEmpty(Long userId, Pageable pageable);
}
5 changes: 5 additions & 0 deletions src/main/resources/db/migration/V2__.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE user_subscription
ADD unsubscribed_at TIMESTAMP WITHOUT TIME ZONE;

ALTER TABLE user_subscription
DROP COLUMN is_subscribed;
Loading