summaryrefslogtreecommitdiff
path: root/pse-server/src/main/java/org/psesquared/server/model/EpisodeAction.java
blob: a7e2375a24faef3956b3a1b4593619ec4ec7205c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.psesquared.server.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.psesquared.server.episode.actions.api.controller.EpisodeActionPost;

/**
 * An action a user took regarding an episode of a podcast.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "episode_actions")
public class EpisodeAction implements Serializable {

  /**
   * The primary key for the table.
   */
  @JsonIgnore
  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE)
  @Column(name = "id", updatable = false)
  private Long id;

  /**
   * The user who is responsible for the action.
   */
  @JsonIgnore
  @ManyToOne(optional = false)
  private User user;

  /**
   * The episode that is affected.
   */
  @JsonIgnore
  @ManyToOne(optional = false)
  private Episode episode;

  /**
   * The timestamp of when this action took place.
   */
  @Column(name = "timestamp",
      nullable = false)
  private LocalDateTime timestamp;

  /**
   * The type of action that happened.
   */
  @JsonProperty(required = true)
  @Column(name = "action",
      nullable = false,
      updatable = false)
  private Action action;

  /**
   * In case of play action: The starting time of the episode.
   */
  @Column(name = "started",
      updatable = false)
  private int started;

  /**
   * In case of play action: The time at which the episode was stopped.
   */
  @Column(name = "position",
      nullable = false,
      updatable = false)
  private int position;

  /**
   * Generates a EpisodeActionPost from the given EpisodeAction for the
   * EpisodeAction Controller.
   *
   * @return The generated EpisodeActionPost
   */
  public EpisodeActionPost toEpisodeActionPost() {
    String podcastUrl = this.getEpisode().getSubscription().getUrl();
    String episodeUrl = this.getEpisode().getUrl();
    String title = this.getEpisode().getTitle();
    String guid = this.getEpisode().getGuid();
    int total = this.getEpisode().getTotal();
    return
        new EpisodeActionPost(podcastUrl, episodeUrl, title, guid, total, this);
  }

}