diff --git a/src/main/java/org/example/vet1177/entities/Comment.java b/src/main/java/org/example/vet1177/entities/Comment.java new file mode 100644 index 00000000..3428658c --- /dev/null +++ b/src/main/java/org/example/vet1177/entities/Comment.java @@ -0,0 +1,59 @@ +package org.example.vet1177.entities; + +import jakarta.persistence.*; +import java.time.Instant; +import java.util.UUID; + +@Entity +@Table(name = "comment") +public class Comment { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.REMOVE) + @JoinColumn(name = "record_id", nullable = false) + private MedicalRecord medicalRecord; + + @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.REMOVE) + @JoinColumn(name = "author_id", nullable = false) + private User author; + + @Column(columnDefinition = "TEXT", nullable = false) + private String body; + + @Column(name = "created_at", updatable = false, nullable = false) + private Instant createdAt; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + + @PrePersist + protected void onCreate() { + createdAt = Instant.now(); + updatedAt = Instant.now(); + } + + @PreUpdate + protected void onUpdate() { + updatedAt = Instant.now(); + } + + public Comment() {} + + public UUID getId() { return id; } + + public MedicalRecord getMedicalRecord() { return medicalRecord; } + public void setMedicalRecord(MedicalRecord medicalRecord) { this.medicalRecord = medicalRecord; } + + public User getAuthor() { return author; } + public void setAuthor(User author) { this.author = author; } + + public String getBody() { return body; } + public void setBody(String body) { this.body = body; } + + public Instant getCreatedAt() { return createdAt; } + public Instant getUpdatedAt() { return updatedAt; } + protected void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; } +} \ No newline at end of file diff --git a/src/main/java/org/example/vet1177/repository/CommentRepository.java b/src/main/java/org/example/vet1177/repository/CommentRepository.java new file mode 100644 index 00000000..3e8755d2 --- /dev/null +++ b/src/main/java/org/example/vet1177/repository/CommentRepository.java @@ -0,0 +1,21 @@ +package org.example.vet1177.repository; + +import org.example.vet1177.entities.Comment; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.UUID; + +@Repository +public interface CommentRepository extends JpaRepository { + + // Alla kommentarer för ett ärende — sorterat äldst först + List findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId); + + // Alla kommentarer skrivna av en specifik användare + List findByAuthorId(UUID authorId); + + // Antal kommentarer på ett ärende + long countByMedicalRecordId(UUID recordId); +} \ No newline at end of file