Skip to content

Commit ee6682f

Browse files
MayuriXxCopilot
andauthored
feat(deleteUser): add delete user by id endpoint (#39)
* feat(deleteUser): add delete user by id endpoint * Update src/main/java/com/xpeho/spring_boot_java_random_user/presentation/controllers/UserController.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * feat(deleteUser): add delete user by id endpoint --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5211dd7 commit ee6682f

21 files changed

Lines changed: 532 additions & 559 deletions

File tree

README.md

Lines changed: 124 additions & 396 deletions
Large diffs are not rendered by default.

src/main/java/com/xpeho/spring_boot_java_random_user/SpringBootJavaRandomUserApplication.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
@SpringBootApplication
88
public class SpringBootJavaRandomUserApplication {
99

10-
public static void main(String[] args) {
11-
Dotenv.configure()
12-
.ignoreIfMissing()
13-
.load()
14-
.entries()
15-
.forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));
10+
private SpringBootJavaRandomUserApplication() {
11+
}
1612

17-
SpringApplication.run(SpringBootJavaRandomUserApplication.class, args);
18-
}
13+
static void main(String[] args) {
14+
Dotenv.configure()
15+
.ignoreIfMissing()
16+
.load()
17+
.entries()
18+
.forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));
19+
20+
SpringApplication.run(SpringBootJavaRandomUserApplication.class, args);
21+
}
1922

2023
}
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.xpeho.spring_boot_java_random_user.data.converters;
2+
23
import com.xpeho.spring_boot_java_random_user.data.models.api.RandomUserResultDAO;
3-
import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity;
44
import com.xpeho.spring_boot_java_random_user.data.models.db.User;
5+
import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity;
56
import org.springframework.stereotype.Service;
67

78

@@ -21,32 +22,34 @@ public User toDao(UserEntity entity) {
2122
user.setNationality(entity.nat());
2223
return user;
2324
}
25+
2426
// DAO -> Domain
2527
public UserEntity toDomain(User user) {
2628
return new UserEntity(
27-
user.getId(),
28-
user.getGender(),
29-
user.getFirstname(),
30-
user.getLastname(),
31-
user.getCivility(),
32-
user.getEmail(),
33-
user.getPhone(),
34-
user.getPicture(),
35-
user.getNationality()
29+
user.getId(),
30+
user.getGender(),
31+
user.getFirstname(),
32+
user.getLastname(),
33+
user.getCivility(),
34+
user.getEmail(),
35+
user.getPhone(),
36+
user.getPicture(),
37+
user.getNationality()
3638
);
3739
}
40+
3841
// API -> Domain
3942
public UserEntity fromApiModel(RandomUserResultDAO model) {
4043
return new UserEntity(
41-
null,
42-
model.getGender(),
43-
model.getFirstName(),
44-
model.getLastName(),
45-
null,
46-
model.getEmail(),
47-
model.getPhone(),
48-
model.getImage(),
49-
null
44+
null,
45+
model.getGender(),
46+
model.getFirstName(),
47+
model.getLastName(),
48+
null,
49+
model.getEmail(),
50+
model.getPhone(),
51+
model.getImage(),
52+
null
5053
);
5154
}
5255
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/models/api/RandomUserNameDAO.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@ public class RandomUserNameDAO {
55
private String first;
66
private String last;
77

8-
public String getTitle() { return title; }
9-
public void setTitle(String title) { this.title = title; }
10-
public String getFirst() { return first; }
11-
public void setFirst(String first) { this.first = first; }
12-
public String getLast() { return last; }
13-
public void setLast(String last) { this.last = last; }
8+
public String getTitle() {
9+
return title;
10+
}
11+
12+
public void setTitle(String title) {
13+
this.title = title;
14+
}
15+
16+
public String getFirst() {
17+
return first;
18+
}
19+
20+
public void setFirst(String first) {
21+
this.first = first;
22+
}
23+
24+
public String getLast() {
25+
return last;
26+
}
27+
28+
public void setLast(String last) {
29+
this.last = last;
30+
}
1431
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/models/api/RandomUserResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.xpeho.spring_boot_java_random_user.data.models.api;
22

33
import com.google.gson.annotations.SerializedName;
4+
45
import java.util.List;
56

67
public class RandomUserResponse {

src/main/java/com/xpeho/spring_boot_java_random_user/data/models/api/RandomUserResultDAO.java

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,51 @@ public class RandomUserResultDAO {
88
private String phone;
99
private String image;
1010

11-
public String getGender() { return gender; }
12-
public void setGender(String gender) { this.gender = gender; }
13-
public String getFirstName() { return firstName; }
14-
public void setFirstName(String firstName) { this.firstName = firstName; }
15-
public String getLastName() { return lastName; }
16-
public void setLastName(String lastName) { this.lastName = lastName; }
17-
public String getEmail() { return email; }
18-
public void setEmail(String email) { this.email = email; }
19-
public String getPhone() { return phone; }
20-
public void setPhone(String phone) { this.phone = phone; }
21-
public String getImage() { return image; }
22-
public void setImage(String image) { this.image = image; }
11+
public String getGender() {
12+
return gender;
13+
}
14+
15+
public void setGender(String gender) {
16+
this.gender = gender;
17+
}
18+
19+
public String getFirstName() {
20+
return firstName;
21+
}
22+
23+
public void setFirstName(String firstName) {
24+
this.firstName = firstName;
25+
}
26+
27+
public String getLastName() {
28+
return lastName;
29+
}
30+
31+
public void setLastName(String lastName) {
32+
this.lastName = lastName;
33+
}
34+
35+
public String getEmail() {
36+
return email;
37+
}
38+
39+
public void setEmail(String email) {
40+
this.email = email;
41+
}
42+
43+
public String getPhone() {
44+
return phone;
45+
}
46+
47+
public void setPhone(String phone) {
48+
this.phone = phone;
49+
}
50+
51+
public String getImage() {
52+
return image;
53+
}
54+
55+
public void setImage(String image) {
56+
this.image = image;
57+
}
2358
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/models/db/User.java

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,75 @@ public User() {
2424
// No initialization needed; fields are populated by Spring Data JDBC after instantiation
2525
}
2626

27-
public Long getId() { return id; }
28-
public void setId(Long id) { this.id = id; }
29-
public String getGender() { return gender; }
30-
public void setGender(String gender) { this.gender = gender; }
31-
public String getFirstname() { return firstname; }
32-
public void setFirstname(String firstname) { this.firstname = firstname; }
33-
public String getLastname() { return lastname; }
34-
public void setLastname(String lastname) { this.lastname = lastname; }
35-
public String getCivility() { return civility; }
36-
public void setCivility(String civility) { this.civility = civility; }
37-
public String getEmail() { return email; }
38-
public void setEmail(String email) { this.email = email; }
39-
public String getPhone() { return phone; }
40-
public void setPhone(String phone) { this.phone = phone; }
41-
public String getPicture() { return picture; }
42-
public void setPicture(String picture) { this.picture = picture; }
43-
public String getNationality() { return nationality; }
44-
public void setNationality(String nationality) { this.nationality = nationality; }
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getGender() {
36+
return gender;
37+
}
38+
39+
public void setGender(String gender) {
40+
this.gender = gender;
41+
}
42+
43+
public String getFirstname() {
44+
return firstname;
45+
}
46+
47+
public void setFirstname(String firstname) {
48+
this.firstname = firstname;
49+
}
50+
51+
public String getLastname() {
52+
return lastname;
53+
}
54+
55+
public void setLastname(String lastname) {
56+
this.lastname = lastname;
57+
}
58+
59+
public String getCivility() {
60+
return civility;
61+
}
62+
63+
public void setCivility(String civility) {
64+
this.civility = civility;
65+
}
66+
67+
public String getEmail() {
68+
return email;
69+
}
70+
71+
public void setEmail(String email) {
72+
this.email = email;
73+
}
74+
75+
public String getPhone() {
76+
return phone;
77+
}
78+
79+
public void setPhone(String phone) {
80+
this.phone = phone;
81+
}
82+
83+
public String getPicture() {
84+
return picture;
85+
}
86+
87+
public void setPicture(String picture) {
88+
this.picture = picture;
89+
}
90+
91+
public String getNationality() {
92+
return nationality;
93+
}
94+
95+
public void setNationality(String nationality) {
96+
this.nationality = nationality;
97+
}
4598
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/services/UserServiceImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.xpeho.spring_boot_java_random_user.data.services;
22

3+
import com.xpeho.spring_boot_java_random_user.data.converters.UserConverter;
4+
import com.xpeho.spring_boot_java_random_user.data.models.db.User;
35
import com.xpeho.spring_boot_java_random_user.data.sources.database.UserRepository;
46
import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity;
57
import com.xpeho.spring_boot_java_random_user.domain.services.UserService;
6-
import com.xpeho.spring_boot_java_random_user.data.models.db.User;
7-
import com.xpeho.spring_boot_java_random_user.data.converters.UserConverter;
88
import org.springframework.stereotype.Service;
99

1010
import java.util.List;
@@ -27,19 +27,24 @@ public List<UserEntity> saveAll(List<UserEntity> users) {
2727
List<User> daoUsers = users.stream().map(userConverter::toDao).toList();
2828
Iterable<User> saved = userRepository.saveAll(daoUsers);
2929
return StreamSupport.stream(saved.spliterator(), false)
30-
.map(userConverter::toDomain)
31-
.toList();
30+
.map(userConverter::toDomain)
31+
.toList();
3232
}
3333

3434
@Override
3535
public Optional<UserEntity> getById(long id) {
3636
return userRepository.findById(id)
37-
.map(userConverter::toDomain);
37+
.map(userConverter::toDomain);
3838
}
3939

4040
@Override
4141
public UserEntity save(UserEntity user) {
4242
User savedUser = userRepository.save(userConverter.toDao(user));
4343
return userConverter.toDomain(savedUser);
4444
}
45+
46+
@Override
47+
public void deleteById(long id) {
48+
userRepository.deleteById(id);
49+
}
4550
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/sources/api/RandomUserProviderImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.xpeho.spring_boot_java_random_user.data.sources.api;
22

3+
import com.xpeho.spring_boot_java_random_user.data.converters.UserConverter;
34
import com.xpeho.spring_boot_java_random_user.data.models.api.RandomUserResponse;
45
import com.xpeho.spring_boot_java_random_user.data.models.api.RandomUserResultDAO;
56
import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity;
67
import com.xpeho.spring_boot_java_random_user.domain.services.RandomUserProvider;
7-
import com.xpeho.spring_boot_java_random_user.data.converters.UserConverter;
88
import org.springframework.stereotype.Service;
99
import retrofit2.Response;
1010

@@ -32,7 +32,7 @@ public List<UserEntity> fetchRandomUsers(int count) throws IOException {
3232
throw new IOException("Failed to parse users from response");
3333
}
3434
return users.stream()
35-
.map(userConverter::fromApiModel)
36-
.toList();
35+
.map(userConverter::fromApiModel)
36+
.toList();
3737
}
3838
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.xpeho.spring_boot_java_random_user.domain.entities;
22

33
public record UserEntity(
4-
Long id,
5-
String gender,
6-
String firstname,
7-
String lastname,
8-
String civility,
9-
String email,
10-
String phone,
11-
String picture,
12-
String nat
13-
) {}
4+
Long id,
5+
String gender,
6+
String firstname,
7+
String lastname,
8+
String civility,
9+
String email,
10+
String phone,
11+
String picture,
12+
String nat
13+
) {
14+
}

0 commit comments

Comments
 (0)