Refactor Article class to extend Publication and add views attribute

This commit is contained in:
Vincent Guillet
2025-05-19 14:44:26 +02:00
parent e368538fed
commit 02b837a0ea

View File

@@ -2,53 +2,23 @@ package com.humanbooster.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.time.LocalDate;
@Entity @Entity
public class Article { public class Article extends Publication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title", nullable = false, length = 100)
private String title;
@Column(name = "content", nullable = false)
private String content;
@ManyToOne @ManyToOne
@JoinColumn(name="author_id", nullable=false) @JoinColumn(name="author_id", nullable = false)
private User author; private User author;
private int views = 0;
public Article() {} public Article() {}
public Article (String title, String content, User author){ public Article (String title, String content, LocalDate publishDate, User author, int views) {
this.title = title; super(title, content, publishDate);
this.content = content;
this.author = author; this.author = author;
} this.views = views;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
} }
public User getAuthor() { public User getAuthor() {
@@ -63,9 +33,9 @@ public class Article {
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("Article ID: ").append(id).append("\n"); sb.append("Article ID: ").append(super.getId()).append("\n");
sb.append("Title: ").append(title).append("\n"); sb.append("Title: ").append(super.getTitle()).append("\n");
sb.append("Content: ").append(content).append("\n"); sb.append("Content: ").append(super.getContent()).append("\n");
sb.append("Author: ").append(author != null ? author.getName() : "Unknown").append("\n"); sb.append("Author: ").append(author != null ? author.getName() : "Unknown").append("\n");
return sb.toString(); return sb.toString();