spring-boot-realworld-examp.../src/main/java/io/spring/infrastructure/repository/MyBatisUserRepository.java

62 lines
1.8 KiB
Java
Raw Normal View History

2017-08-18 16:08:27 +07:00
package io.spring.infrastructure.repository;
2017-08-08 10:01:06 +07:00
2017-08-16 15:25:08 +07:00
import io.spring.core.user.FollowRelation;
2017-08-08 10:01:06 +07:00
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
2017-08-18 16:08:27 +07:00
import io.spring.infrastructure.mybatis.mapper.UserMapper;
2017-08-08 10:01:06 +07:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class MyBatisUserRepository implements UserRepository {
private final UserMapper userMapper;
@Autowired
public MyBatisUserRepository(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public void save(User user) {
2017-08-14 13:27:36 +07:00
if (userMapper.findById(user.getId()) == null) {
userMapper.insert(user);
} else {
userMapper.update(user);
}
}
@Override
public Optional<User> findById(String id) {
return Optional.ofNullable(userMapper.findById(id));
2017-08-08 10:01:06 +07:00
}
@Override
public Optional<User> findByUsername(String username) {
return Optional.ofNullable(userMapper.findByUsername(username));
}
@Override
public Optional<User> findByEmail(String email) {
return Optional.ofNullable(userMapper.findByEmail(email));
}
2017-08-16 15:25:08 +07:00
@Override
public void saveRelation(FollowRelation followRelation) {
2017-08-16 16:13:24 +07:00
if (!findRelation(followRelation.getUserId(), followRelation.getTargetId()).isPresent()) {
userMapper.saveRelation(followRelation);
}
2017-08-16 15:25:08 +07:00
}
@Override
public Optional<FollowRelation> findRelation(String userId, String targetId) {
return Optional.ofNullable(userMapper.findRelation(userId, targetId));
}
2017-08-16 16:13:24 +07:00
@Override
public void removeRelation(FollowRelation followRelation) {
userMapper.deleteRelation(followRelation);
}
2017-08-08 10:01:06 +07:00
}