should create article success
This commit is contained in:
@@ -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