Created
March 28, 2021 16:30
-
-
Save mehmetcemyucel/b86acf6dc5baf875f8a72b54bd81be31 to your computer and use it in GitHub Desktop.
spring-native-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cem.springnativeexample.service; | |
import com.cem.springnativeexample.controller.model.User; | |
import com.cem.springnativeexample.repository.UserRepository; | |
import com.cem.springnativeexample.repository.entity.UserEntity; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.stream.Collectors; | |
@Service | |
public class UserService { | |
private final UserRepository userRepository; | |
public UserService(UserRepository userRepository) { | |
this.userRepository = userRepository; | |
} | |
public Optional<User> getUser(Long id) { | |
Optional<UserEntity> userEntity = userRepository.findById(id); | |
return User.of(userEntity); | |
} | |
public List<User> getUsers() { | |
return userRepository.findAll().stream() | |
.map(userEntity -> User.of(userEntity)) | |
.collect(Collectors.toList()); | |
} | |
public void createUser(User user) { | |
UserEntity userEntity = UserEntity.of(user); | |
userRepository.save(userEntity); | |
} | |
public void deleteUser(Long id) { | |
userRepository.deleteById(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment