should create article success
This commit is contained in:
67
src/main/java/io/spring/api/ArticlesApi.java
Normal file
67
src/main/java/io/spring/api/ArticlesApi.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package io.spring.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import io.spring.api.exception.InvalidRequestException;
|
||||
import io.spring.application.article.ArticleQueryService;
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.user.User;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/articles")
|
||||
public class ArticlesApi {
|
||||
private ArticleRepository articleRepository;
|
||||
private ArticleQueryService articleQueryService;
|
||||
|
||||
@Autowired
|
||||
public ArticlesApi(ArticleRepository articleRepository, ArticleQueryService articleQueryService) {
|
||||
this.articleRepository = articleRepository;
|
||||
this.articleQueryService = articleQueryService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity createArticle(@Valid @RequestBody NewArticleParam newArticleParam,
|
||||
BindingResult bindingResult,
|
||||
@AuthenticationPrincipal User user) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
throw new InvalidRequestException(bindingResult);
|
||||
}
|
||||
|
||||
Article article = new Article(
|
||||
articleRepository.toSlug(
|
||||
newArticleParam.getTitle()),
|
||||
newArticleParam.getTitle(),
|
||||
newArticleParam.getDescription(),
|
||||
newArticleParam.getBody(),
|
||||
newArticleParam.getTagList(),
|
||||
user.getId());
|
||||
articleRepository.save(article);
|
||||
return ResponseEntity.ok(articleQueryService.findById(article.getId(), user).get());
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@JsonRootName("article")
|
||||
@NoArgsConstructor
|
||||
class NewArticleParam {
|
||||
@NotBlank(message = "can't be empty")
|
||||
private String title;
|
||||
@NotBlank(message = "can't be empty")
|
||||
private String description;
|
||||
@NotBlank(message = "can't be empty")
|
||||
private String body;
|
||||
private String[] tagList;
|
||||
}
|
||||
31
src/main/java/io/spring/application/article/ArticleData.java
Normal file
31
src/main/java/io/spring/application/article/ArticleData.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import io.spring.application.profile.ProfileData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonRootName("article")
|
||||
public class ArticleData {
|
||||
private String id;
|
||||
private String slug;
|
||||
private String title;
|
||||
private String description;
|
||||
private String body;
|
||||
private boolean favorited;
|
||||
private int favoritesCount;
|
||||
private DateTime createdAt;
|
||||
private DateTime updatedAt;
|
||||
private List<String> tagList;
|
||||
@JsonProperty("author")
|
||||
private ProfileData profileData;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Mapper
|
||||
@Component
|
||||
public interface ArticleFavoritesQueryService {
|
||||
boolean isUserFavorite(@Param("userId") String userId, @Param("articleId") String articleId);
|
||||
|
||||
int articleFavoriteCount(@Param("articleId") String articleId);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import io.spring.application.profile.UserRelationshipQueryService;
|
||||
import io.spring.core.user.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ArticleQueryService {
|
||||
private ArticleReadService articleReadService;
|
||||
private UserRelationshipQueryService userRelationshipQueryService;
|
||||
private ArticleFavoritesQueryService articleFavoritesQueryService;
|
||||
|
||||
@Autowired
|
||||
public ArticleQueryService(ArticleReadService articleReadService,
|
||||
UserRelationshipQueryService userRelationshipQueryService,
|
||||
ArticleFavoritesQueryService articleFavoritesQueryService) {
|
||||
this.articleReadService = articleReadService;
|
||||
this.userRelationshipQueryService = userRelationshipQueryService;
|
||||
this.articleFavoritesQueryService = articleFavoritesQueryService;
|
||||
}
|
||||
|
||||
public Optional<ArticleData> findById(String id, User user) {
|
||||
ArticleData articleData = articleReadService.ofId(id);
|
||||
if (articleData == null) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
articleData.setFavorited(articleFavoritesQueryService.isUserFavorite(user.getId(), id));
|
||||
articleData.setFavoritesCount(articleFavoritesQueryService.articleFavoriteCount(id));
|
||||
articleData.getProfileData().setFollowing(
|
||||
userRelationshipQueryService.isUserFollowing(
|
||||
user.getId(),
|
||||
articleData.getProfileData().getId()));
|
||||
return Optional.of(articleData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface ArticleReadService {
|
||||
ArticleData ofId(@Param("id") String id);
|
||||
}
|
||||
18
src/main/java/io/spring/application/profile/ProfileData.java
Normal file
18
src/main/java/io/spring/application/profile/ProfileData.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package io.spring.application.profile;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ProfileData {
|
||||
@JsonIgnore
|
||||
private String id;
|
||||
private String username;
|
||||
private String bio;
|
||||
private String image;
|
||||
private boolean following;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package io.spring.application.profile;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface UserRelationshipQueryService {
|
||||
boolean isUserFollowing(@Param("userId") String userId, @Param("anotherUserId") String anotherUserId);
|
||||
}
|
||||
@@ -11,10 +11,8 @@ import javax.persistence.Id;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@JsonRootName("user")
|
||||
public class UserData {
|
||||
@Id
|
||||
private String id;
|
||||
private String email;
|
||||
private String username;
|
||||
|
||||
@@ -16,12 +16,12 @@ public class UserQueryService {
|
||||
}
|
||||
|
||||
public UserWithToken fetchNewAuthenticatedUser(String username) {
|
||||
UserData userData = userReadService.findOne(username);
|
||||
UserData userData = userReadService.findByUsername(username);
|
||||
return new UserWithToken(userData, jwtService.toToken(userData));
|
||||
}
|
||||
|
||||
public UserWithToken fetchCurrentUser(String username, String token) {
|
||||
return new UserWithToken(userReadService.findOne(username), token);
|
||||
return new UserWithToken(userReadService.findByUsername(username), token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package io.spring.application.user;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public interface UserReadService extends CrudRepository<UserData, String> {
|
||||
@Component
|
||||
@Mapper
|
||||
public interface UserReadService {
|
||||
|
||||
UserData findByUsername(@Param("username") String username);
|
||||
}
|
||||
|
||||
|
||||
41
src/main/java/io/spring/core/article/Article.java
Normal file
41
src/main/java/io/spring/core/article/Article.java
Normal file
@@ -0,0 +1,41 @@
|
||||
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 slug, String title, String description, String body, String[] tagList, String userId) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.slug = slug;
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
11
src/main/java/io/spring/core/article/ArticleRepository.java
Normal file
11
src/main/java/io/spring/core/article/ArticleRepository.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package io.spring.core.article;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ArticleRepository {
|
||||
String toSlug(String title);
|
||||
|
||||
void save(Article article);
|
||||
|
||||
Optional<Article> findById(String id);
|
||||
}
|
||||
20
src/main/java/io/spring/core/article/Tag.java
Normal file
20
src/main/java/io/spring/core/article/Tag.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package io.spring.core.article;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(of = "name")
|
||||
public class Tag {
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public Tag(String name) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(of = {"username"})
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class User {
|
||||
private String id;
|
||||
private String email;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.spring.infrastructure.article;
|
||||
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.Tag;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface ArticleMapper {
|
||||
void insert(@Param("article") Article article);
|
||||
|
||||
Article findById(@Param("id") String id);
|
||||
|
||||
boolean findTag(@Param("tagName") String tagName);
|
||||
|
||||
void insertTag(@Param("tag") Tag tag);
|
||||
|
||||
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.spring.infrastructure.article;
|
||||
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.article.Tag;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public class MyBatisArticleRepository implements ArticleRepository {
|
||||
private ArticleMapper articleMapper;
|
||||
|
||||
public MyBatisArticleRepository(ArticleMapper articleMapper) {
|
||||
this.articleMapper = articleMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSlug(String title) {
|
||||
return title.toLowerCase().replace(' ', '-');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Article article) {
|
||||
articleMapper.insert(article);
|
||||
for (Tag tag : article.getTags()) {
|
||||
if (!articleMapper.findTag(tag.getName())) {
|
||||
articleMapper.insertTag(tag);
|
||||
}
|
||||
articleMapper.insertArticleTagRelation(article.getId(), tag.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Article> findById(String id) {
|
||||
return Optional.ofNullable(articleMapper.findById(id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.spring.infrastructure.mybatis;
|
||||
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@MappedTypes(DateTime.class)
|
||||
public class DateTimeHandler implements TypeHandler<DateTime> {
|
||||
|
||||
private static final Calendar UTC_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
@Override
|
||||
public void setParameter(PreparedStatement ps, int i, DateTime parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setTimestamp(i, parameter != null ? new Timestamp(parameter.getMillis()) : null, UTC_CALENDAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime getResult(ResultSet rs, String columnName) throws SQLException {
|
||||
Timestamp timestamp = rs.getTimestamp(columnName, UTC_CALENDAR);
|
||||
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
Timestamp timestamp = rs.getTimestamp(columnIndex, UTC_CALENDAR);
|
||||
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
Timestamp ts = cs.getTimestamp(columnIndex, UTC_CALENDAR);
|
||||
return ts != null ? new DateTime(ts.getTime()) : null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user