Skip to content

Commit 0658f50

Browse files
committed
Reorganize packages
1 parent 145afed commit 0658f50

File tree

84 files changed

+1138
-1207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1138
-1207
lines changed

src/main/java/com/github/throyer/common/springboot/configurations/SpringSecurityConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import static org.springframework.http.HttpMethod.POST;
1616
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
1717

18-
import com.github.throyer.common.springboot.domain.services.security.SecurityService;
18+
import com.github.throyer.common.springboot.domain.session.service.SessionService;
1919
import com.github.throyer.common.springboot.middlewares.AuthorizationMiddleware;
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
@@ -42,7 +42,7 @@
4242
public class SpringSecurityConfiguration {
4343

4444
@Autowired
45-
private SecurityService securityService;
45+
private SessionService sessionService;
4646

4747
@Autowired
4848
private BCryptPasswordEncoder encoder;
@@ -62,7 +62,7 @@ public class Api extends WebSecurityConfigurerAdapter {
6262

6363
@Override
6464
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
65-
auth.userDetailsService(securityService)
65+
auth.userDetailsService(sessionService)
6666
.passwordEncoder(encoder);
6767
}
6868

@@ -121,7 +121,7 @@ public class App extends WebSecurityConfigurerAdapter {
121121
@Override
122122
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
123123
auth.
124-
userDetailsService(securityService)
124+
userDetailsService(sessionService)
125125
.passwordEncoder(encoder);
126126
}
127127

src/main/java/com/github/throyer/common/springboot/controllers/api/RecoveriesController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import static org.springframework.http.HttpStatus.NO_CONTENT;
44

5-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryConfirmService;
6-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryService;
7-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryUpdateService;
8-
import com.github.throyer.common.springboot.domain.services.user.dto.RecoveryConfirm;
9-
import com.github.throyer.common.springboot.domain.services.user.dto.RecoveryRequest;
10-
import com.github.throyer.common.springboot.domain.services.user.dto.RecoveryUpdate;
5+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryConfirmService;
6+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryService;
7+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryUpdateService;
8+
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryConfirm;
9+
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryRequest;
10+
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryUpdate;
1111

1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.web.bind.annotation.PostMapping;

src/main/java/com/github/throyer/common/springboot/controllers/api/RolesController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import java.util.List;
66

7-
import com.github.throyer.common.springboot.domain.models.entity.Role;
8-
import com.github.throyer.common.springboot.domain.repositories.RoleRepository;
7+
import com.github.throyer.common.springboot.domain.role.entity.Role;
8+
import com.github.throyer.common.springboot.domain.role.repository.RoleRepository;
99
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
1010

1111
import org.springframework.beans.factory.annotation.Autowired;

src/main/java/com/github/throyer/common/springboot/controllers/api/SessionsController.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import javax.validation.Valid;
44

5-
import com.github.throyer.common.springboot.domain.services.security.SessionService;
6-
import com.github.throyer.common.springboot.domain.services.security.dto.RefreshTokenRequest;
7-
import com.github.throyer.common.springboot.domain.services.security.dto.RefreshTokenResponse;
8-
import com.github.throyer.common.springboot.domain.services.security.dto.TokenRequest;
9-
import com.github.throyer.common.springboot.domain.services.security.dto.TokenResponse;
5+
import com.github.throyer.common.springboot.domain.session.model.RefreshTokenRequest;
6+
import com.github.throyer.common.springboot.domain.session.model.RefreshTokenResponse;
7+
import com.github.throyer.common.springboot.domain.session.model.TokenRequest;
8+
import com.github.throyer.common.springboot.domain.session.model.TokenResponse;
9+
import com.github.throyer.common.springboot.domain.session.service.CreateTokenService;
10+
import com.github.throyer.common.springboot.domain.session.service.RefreshTokenService;
11+
import static com.github.throyer.common.springboot.utils.Responses.ok;
1012

1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.http.ResponseEntity;
@@ -20,15 +22,20 @@
2022
public class SessionsController {
2123

2224
@Autowired
23-
private SessionService service;
25+
private CreateTokenService createService;
26+
27+
@Autowired
28+
private RefreshTokenService refreshService;
2429

2530
@PostMapping
2631
public ResponseEntity<TokenResponse> create(@RequestBody @Valid TokenRequest request) {
27-
return service.create(request);
32+
var token = createService.create(request);
33+
return ok(token);
2834
}
2935

3036
@PostMapping("/refresh")
3137
public ResponseEntity<RefreshTokenResponse> refresh(@RequestBody @Valid RefreshTokenRequest request) {
32-
return service.refresh(request);
38+
var token = refreshService.refresh(request);
39+
return ok(token);
3340
}
3441
}

src/main/java/com/github/throyer/common/springboot/controllers/api/UsersController.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
import static org.springframework.http.HttpStatus.CREATED;
44
import static org.springframework.http.HttpStatus.NO_CONTENT;
55

6-
import com.github.throyer.common.springboot.domain.models.entity.User;
7-
import com.github.throyer.common.springboot.domain.models.pagination.Page;
8-
9-
import com.github.throyer.common.springboot.domain.services.user.CreateUserService;
10-
import com.github.throyer.common.springboot.domain.services.user.FindUserService;
11-
import com.github.throyer.common.springboot.domain.services.user.RemoveUserService;
12-
import com.github.throyer.common.springboot.domain.services.user.UpdateUserService;
13-
import com.github.throyer.common.springboot.domain.services.user.dto.CreateUserApi;
14-
import com.github.throyer.common.springboot.domain.services.user.dto.UpdateUser;
15-
import com.github.throyer.common.springboot.domain.services.user.dto.UserDetails;
6+
import static com.github.throyer.common.springboot.utils.Responses.created;
167
import static com.github.throyer.common.springboot.utils.Responses.ok;
8+
9+
import com.github.throyer.common.springboot.domain.pagination.model.Page;
10+
import com.github.throyer.common.springboot.domain.user.service.FindUserService;
11+
import com.github.throyer.common.springboot.domain.user.service.RemoveUserService;
12+
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
13+
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
14+
import com.github.throyer.common.springboot.domain.user.model.UpdateUserProps;
15+
import com.github.throyer.common.springboot.domain.user.service.FindUserByIdService;
16+
import com.github.throyer.common.springboot.domain.user.service.UpdateUserService;
17+
import com.github.throyer.common.springboot.domain.user.model.UserDetails;
18+
1719
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
20+
1821
import java.util.Optional;
1922

2023
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,18 +37,27 @@
3437
@RestController
3538
@RequestMapping("/api/users")
3639
public class UsersController {
37-
38-
@Autowired
39-
private CreateUserService createService;
40-
41-
@Autowired
42-
private UpdateUserService updateService;
43-
44-
@Autowired
45-
private RemoveUserService removeService;
46-
40+
41+
private final CreateUserService createService;
42+
private final UpdateUserService updateService;
43+
private final RemoveUserService removeService;
44+
private final FindUserService findService;
45+
private final FindUserByIdService findByIdService;
46+
4747
@Autowired
48-
private FindUserService findService;
48+
public UsersController(
49+
CreateUserService createService,
50+
UpdateUserService updateService,
51+
RemoveUserService removeService,
52+
FindUserService findService,
53+
FindUserByIdService findByIdService
54+
) {
55+
this.createService = createService;
56+
this.updateService = updateService;
57+
this.removeService = removeService;
58+
this.findService = findService;
59+
this.findByIdService = findByIdService;
60+
}
4961

5062
@GetMapping
5163
@SecurityRequirement(name = "token")
@@ -54,38 +66,42 @@ public ResponseEntity<Page<UserDetails>> index(
5466
Optional<Integer> page,
5567
Optional<Integer> size
5668
) {
57-
var result = findService.findAll(page, size);
58-
return ok(result);
69+
var content = findService.findAll(page, size);
70+
return ok(content);
5971
}
6072

6173
@GetMapping("/{id}")
6274
@SecurityRequirement(name = "token")
6375
@PreAuthorize("hasAnyAuthority('ADM', 'USER')")
6476
public ResponseEntity<UserDetails> show(@PathVariable Long id) {
65-
return findService.find(id);
77+
var user = findByIdService.find(id);
78+
return ok(user);
6679
}
6780

6881
@PostMapping
6982
@ResponseStatus(CREATED)
70-
public ResponseEntity<UserDetails> save(@Validated @RequestBody CreateUserApi body) {
71-
return createService.create(body);
83+
public ResponseEntity<UserDetails> save(
84+
@Validated @RequestBody CreateUserProps body
85+
) {
86+
var user = createService.create(body);
87+
return created(user, "api/users");
7288
}
7389

7490
@PutMapping("/{id}")
7591
@SecurityRequirement(name = "token")
7692
@PreAuthorize("hasAnyAuthority('ADM', 'USER')")
7793
public ResponseEntity<UserDetails> update(
7894
@PathVariable Long id,
79-
@RequestBody @Validated UpdateUser body
95+
@RequestBody @Validated UpdateUserProps body
8096
) {
81-
return updateService.update(id, body);
97+
var user = updateService.update(id, body);
98+
return ok(user);
8299
}
83100

84101
@DeleteMapping("/{id}")
85102
@ResponseStatus(NO_CONTENT)
86103
@SecurityRequirement(name = "token")
87-
@PreAuthorize("hasAnyAuthority('ADM')")
88-
public ResponseEntity<User> destroy(@PathVariable Long id) {
89-
return removeService.remove(id);
104+
public void destroy(@PathVariable Long id) {
105+
removeService.remove(id);
90106
}
91107
}

src/main/java/com/github/throyer/common/springboot/controllers/app/RecoveryController.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryConfirmService;
4-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryService;
5-
import com.github.throyer.common.springboot.domain.services.recovery.RecoveryUpdateService;
6-
import com.github.throyer.common.springboot.domain.services.user.dto.Codes;
7-
import com.github.throyer.common.springboot.domain.services.user.dto.RecoveryRequest;
8-
import com.github.throyer.common.springboot.domain.services.user.dto.Update;
3+
import static com.github.throyer.common.springboot.utils.Responses.validate;
4+
5+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryConfirmService;
6+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryService;
7+
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryUpdateService;
8+
import com.github.throyer.common.springboot.domain.recovery.model.Codes;
9+
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryRequest;
10+
import com.github.throyer.common.springboot.domain.recovery.model.Update;
11+
import com.github.throyer.common.springboot.domain.shared.Type;
12+
import com.github.throyer.common.springboot.utils.Toasts;
13+
914
import javax.validation.Valid;
15+
1016
import org.springframework.beans.factory.annotation.Autowired;
1117
import org.springframework.stereotype.Controller;
1218
import org.springframework.ui.Model;
1319
import org.springframework.validation.BindingResult;
1420
import org.springframework.web.bind.annotation.GetMapping;
1521
import org.springframework.web.bind.annotation.PostMapping;
1622
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.server.ResponseStatusException;
1724
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1825

1926
@Controller
@@ -41,7 +48,18 @@ public String index(
4148
BindingResult result,
4249
Model model
4350
) {
44-
return recoveryService.recovery(recovery, result, model);
51+
52+
if (validate(model, recovery, "recovery", result)) {
53+
return "app/recovery/index";
54+
}
55+
56+
var email = recovery.getEmail();
57+
58+
recoveryService.recovery(email);
59+
60+
model.addAttribute("codes", new Codes(email));
61+
62+
return "app/recovery/confirm";
4563
}
4664

4765
@PostMapping("/confirm")
@@ -51,7 +69,24 @@ public String confirm(
5169
RedirectAttributes redirect,
5270
Model model
5371
) {
54-
return confirmService.confirm(codes, result, model, redirect);
72+
73+
if (validate(model, codes, "recovery", result)) {
74+
return "app/recovery/confirm";
75+
}
76+
77+
try {
78+
79+
confirmService.confirm(codes.getEmail(), codes.code());
80+
return "redirect:/app/recovery/update";
81+
} catch (ResponseStatusException exception) {
82+
83+
Toasts.add(model, "Código expirado ou invalido.", Type.DANGER);
84+
model.addAttribute("confirm", codes);
85+
}
86+
87+
model.addAttribute("update", new Update(codes));
88+
89+
return "app/recovery/update";
5590
}
5691

5792
@PostMapping("/update")
@@ -61,6 +96,21 @@ public String update(
6196
RedirectAttributes redirect,
6297
Model model
6398
) {
64-
return updateService.update(update, result, model, redirect);
99+
update.validate(result);
100+
101+
if (validate(model, update, "update", result)) {
102+
return "app/recovery/update";
103+
}
104+
105+
try {
106+
updateService.update(update.getEmail(), update.code(), update.getPassword());
107+
} catch (ResponseStatusException exception) {
108+
Toasts.add(model, "Código expirado ou invalido.", Type.DANGER);
109+
model.addAttribute("update", update);
110+
return "app/recovery/update";
111+
}
112+
113+
Toasts.add(redirect, "Sua senha foi atualizada com sucesso.", Type.SUCCESS);
114+
return "redirect:/app/login";
65115
}
66116
}
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import javax.validation.Valid;
3+
import static com.github.throyer.common.springboot.domain.shared.Type.SUCCESS;
4+
import static com.github.throyer.common.springboot.utils.Responses.validate;
5+
6+
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
7+
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
8+
import com.github.throyer.common.springboot.utils.Toasts;
49

5-
import com.github.throyer.common.springboot.domain.services.user.CreateUserService;
6-
import com.github.throyer.common.springboot.domain.services.user.dto.CreateUserApp;
10+
import javax.validation.Valid;
711

812
import org.springframework.beans.factory.annotation.Autowired;
913
import org.springframework.stereotype.Controller;
@@ -17,23 +21,32 @@
1721
@Controller
1822
@RequestMapping("/app/register")
1923
public class RegisterController {
20-
24+
2125
@Autowired
2226
private CreateUserService service;
2327

2428
@GetMapping(produces = "text/html")
2529
public String index(Model model) {
26-
model.addAttribute("user", new CreateUserApp());
30+
model.addAttribute("user", new CreateUserProps());
2731
return "app/register/index";
2832
}
2933

3034
@PostMapping(produces = "text/html")
3135
public String create(
32-
@Valid CreateUserApp user,
36+
@Valid CreateUserProps props,
3337
BindingResult result,
3438
RedirectAttributes redirect,
3539
Model model
3640
) {
37-
return service.create(user, result, redirect, model);
41+
42+
if (validate(model, props, "user", result)) {
43+
return "app/register/index";
44+
}
45+
46+
service.create(props);
47+
48+
Toasts.add(redirect, "Cadastro realizado com sucesso.", SUCCESS);
49+
50+
return "redirect:/app/login";
3851
}
3952
}

0 commit comments

Comments
 (0)