1 | package tech.techharbor.Service.Impl;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Service;
|
---|
4 | import tech.techharbor.Model.Exceptions.UserNotFoundException;
|
---|
5 | import tech.techharbor.Model.UserTableModel;
|
---|
6 | import tech.techharbor.Repository.UserTableRepository;
|
---|
7 | import tech.techharbor.Service.UserService;
|
---|
8 |
|
---|
9 | @Service
|
---|
10 | public class UserServiceImpl implements UserService {
|
---|
11 |
|
---|
12 | private final UserTableRepository userTableRepository;
|
---|
13 |
|
---|
14 | public UserServiceImpl(UserTableRepository userTableRepository) {
|
---|
15 | this.userTableRepository = userTableRepository;
|
---|
16 | }
|
---|
17 |
|
---|
18 | @Override
|
---|
19 | public UserTableModel findById(Integer id) {
|
---|
20 | return this.userTableRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user ID: " + id));
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public UserTableModel create(String name, String username, String email, String password, String phoneNumber) {
|
---|
25 | UserTableModel user = new UserTableModel(username, name, email, password, phoneNumber);
|
---|
26 | return this.userTableRepository.save(user);
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public UserTableModel update(Integer id, String name, String username, String email, String password, String phoneNumber) {
|
---|
31 | UserTableModel user = this.userTableRepository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
|
---|
32 | user.setNameUser(name);
|
---|
33 | user.setUsername(username);
|
---|
34 | user.setEmail(email);
|
---|
35 | user.setPassword(password);
|
---|
36 | user.setPhoneNumber(phoneNumber);
|
---|
37 | return this.userTableRepository.save(user);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public UserTableModel findByUsername(String username) {
|
---|
42 | return this.userTableRepository.findByUsername(username);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void delete(Integer id) {
|
---|
47 | this.userTableRepository.deleteById(id);
|
---|
48 | }
|
---|
49 | }
|
---|