spring-boot-realworld-examp.../src/main/java/io/spring/core/article/Article.java
2020-11-26 16:01:13 +08:00

63 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package io.spring.core.article;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
@Getter
@NoArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class Article {
private String userId;
private String id;
private String slug;
private String title;
private String description;
private String body;
private List<Tag> tags;
private DateTime createdAt;
private DateTime updatedAt;
public Article(String title, String description, String body, String[] tagList, String userId) {
this(title, description, body, tagList, userId, new DateTime());
}
public Article(String title, String description, String body, String[] tagList, String userId, DateTime createdAt) {
this.id = UUID.randomUUID().toString();
this.slug = toSlug(title);
this.title = title;
this.description = description;
this.body = body;
this.tags = Arrays.stream(tagList).collect(toSet()).stream().map(Tag::new).collect(toList());
this.userId = userId;
this.createdAt = createdAt;
this.updatedAt = createdAt;
}
public void update(String title, String description, String body) {
if (!"".equals(title)) {
this.title = title;
this.slug = toSlug(title);
}
if (!"".equals(description)) {
this.description = description;
}
if (!"".equals(body)) {
this.body = body;
}
this.updatedAt = new DateTime();
}
public static String toSlug(String title) {
return title.toLowerCase().replaceAll("[\\&|[\\uFE30-\\uFFA0]|\\|\\”|\\s\\?\\,\\.]+", "-");
}
}