Skip to content

Post API repurposement and research #1

Description

@Orlando-c

Original code (source f1nnC):

package com.nighthawk.spring_portfolio.mvc.chatboard;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@entity
public class Post {

@Id @GeneratedValue
@Column(name = "post_id")
private Long Id;

private String title;
private String content;

private String writer;


public Long getId() {
    return Id;
}

public void setId(Long id) {
    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 String getWriter() { return writer; }

public void setWriter(String writer) { this.writer = writer; }

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Post post = (Post) o;
    return Objects.equals(Id, post.Id) &&
            Objects.equals(title, post.title) &&
            Objects.equals(content, post.content) &&
            Objects.equals(writer, post.writer);
}

@Override
public int hashCode() {
    return Objects.hash(Id, title, content, writer);
}

}

This is a Java class named Post that represents a post in a chat board. It's annotated with @Entity, indicating that it's a JPA entity and should be mapped to a database table. The class has four fields: Id, title, content, and writer.

Here's a brief explanation of each field:

  • Id: This is the unique identifier for each post. It's annotated with @Id and @GeneratedValue, indicating that it's the primary key and its value should be automatically generated by the database.
  • title: This is the title of the post.
  • content: This is the content of the post.
  • writer: This is the writer of the post.

The class also includes getter and setter methods for each field, allowing other parts of your application to interact with the data. The equals and hashCode methods are overridden to provide custom equality checks based on the fields of the Post class.

To repurpose this for a chat site like Discord, I might want to consider the following changes:

  1. Add a timestamp: Discord messages typically have timestamps to show when they were sent. You could add a timestamp field to your Post class to store this information.

  2. Rename the class and fields: The term 'Post' is often used for forum-like applications, where users create standalone posts with titles. In a chat application, you might rename the Post class to Message, the title field to username (or link this to a separate User entity), and the content field to message.

  3. Add a channel or server field: In Discord, messages are sent in specific channels within servers. You could add a channel or server field (or both) to your Message class to store this information.

Here's how the code might look after these changes:

package com.nighthawk.discord_chat.mvc;

import java.util.Objects;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Message {

    @Id @GeneratedValue
    @Column(name = "message_id")
    private Long Id;

    private String username;
    private String message;
    private LocalDateTime timestamp;

    private String channel;

    // getters and setters...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Message message = (Message) o;
        return Objects.equals(Id, message.Id) &&
                Objects.equals(username, message.username) &&
                Objects.equals(message, message.message) &&
                Objects.equals(timestamp, message.timestamp) &&
                Objects.equals(channel, message.channel);
    }

    @Override
    public int hashCode() {
        return Objects.hash(Id, username, message, timestamp, channel);
    }
}

This is a simplified example. Our actual application would have separate User, Channel, and Place entities, with relationships between them. But this should give me a good

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions