2017-08-15 09:47:18 +07:00
|
|
|
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;
|
|
|
|
|
2017-08-15 13:17:54 +07:00
|
|
|
public Article(String title, String description, String body, String[] tagList, String userId) {
|
2017-08-15 09:47:18 +07:00
|
|
|
this.id = UUID.randomUUID().toString();
|
2017-08-15 13:17:54 +07:00
|
|
|
this.slug = toSlug(title);
|
2017-08-15 09:47:18 +07:00
|
|
|
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 = new DateTime();
|
|
|
|
this.updatedAt = new DateTime();
|
|
|
|
}
|
|
|
|
|
2017-08-15 13:17:54 +07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String toSlug(String title) {
|
|
|
|
return title.toLowerCase().replace(' ', '-');
|
|
|
|
}
|
2017-08-15 09:47:18 +07:00
|
|
|
}
|