-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b219941
commit 8257abb
Showing
9 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
application/src/main/java/br/com/ifsp/tickets/app/auth/update/IUpdateUserUseCase.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package br.com.ifsp.tickets.app.auth.update; | ||
|
||
import br.com.ifsp.tickets.app.IUseCase; | ||
import br.com.ifsp.tickets.app.auth.get.UserOutput; | ||
|
||
public interface IUpdateUserUseCase extends IUseCase<UpdateUserInput, UserOutput> { | ||
} |
20 changes: 20 additions & 0 deletions
20
application/src/main/java/br/com/ifsp/tickets/app/auth/update/UpdateUserInput.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package br.com.ifsp.tickets.app.auth.update; | ||
|
||
import br.com.ifsp.tickets.domain.user.User; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record UpdateUserInput( | ||
User author, | ||
String id, | ||
String name, | ||
String bio, | ||
LocalDate birthDate, | ||
String document | ||
) { | ||
|
||
public static UpdateUserInput of(User author, String id, String name, String bio, LocalDate birthDate, String document) { | ||
return new UpdateUserInput(author, id, name, bio, birthDate, document); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
application/src/main/java/br/com/ifsp/tickets/app/auth/update/UpdateUserUseCase.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package br.com.ifsp.tickets.app.auth.update; | ||
|
||
import br.com.ifsp.tickets.app.auth.get.UserOutput; | ||
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalResourceAccessException; | ||
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException; | ||
import br.com.ifsp.tickets.domain.shared.validation.handler.Notification; | ||
import br.com.ifsp.tickets.domain.user.IUserGateway; | ||
import br.com.ifsp.tickets.domain.user.User; | ||
import br.com.ifsp.tickets.domain.user.UserID; | ||
import br.com.ifsp.tickets.domain.user.vo.RG; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class UpdateUserUseCase implements IUpdateUserUseCase { | ||
|
||
private final IUserGateway userGateway; | ||
|
||
public UpdateUserUseCase(IUserGateway userGateway) { | ||
this.userGateway = userGateway; | ||
} | ||
|
||
@Override | ||
public UserOutput execute(UpdateUserInput anIn) { | ||
final User author = anIn.author(); | ||
final UserID target = UserID.with(anIn.id()); | ||
final String name = anIn.name(); | ||
final String bio = anIn.bio(); | ||
final LocalDate birthDate = anIn.birthDate(); | ||
final RG document = new RG(anIn.document()); | ||
|
||
final Notification notification = Notification.create("Could not update aggregate User"); | ||
|
||
final User targetUser; | ||
if (author.getId().equals(target)) targetUser = author; | ||
else if (!author.canManageAnyUsers()) | ||
throw new IllegalResourceAccessException("User does not have permission to manage other users"); | ||
else | ||
targetUser = this.userGateway.findById(target).orElseThrow(() -> NotFoundException.with(User.class, target)); | ||
|
||
targetUser.updateProfile(name, bio, document, birthDate); | ||
|
||
targetUser.validate(notification); | ||
notification.throwPossibleErrors(); | ||
|
||
return UserOutput.from(this.userGateway.update(targetUser), author.getId(), author.getRole().getPermissions()); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
...rc/main/java/br/com/ifsp/tickets/infra/contexts/user/models/update/UpdateUserRequest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package br.com.ifsp.tickets.infra.contexts.user.models.update; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public record UpdateUserRequest( | ||
@JsonProperty("name") | ||
String name, | ||
@JsonProperty("bio") | ||
String bio, | ||
@JsonProperty("birth_date") | ||
String birthDate, | ||
@JsonProperty("document") | ||
String document | ||
) { | ||
|
||
public LocalDate getBirthDate() { | ||
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | ||
return LocalDate.parse(birthDate, formatter); | ||
} | ||
|
||
} |