Skip to content

Commit

Permalink
feat(app): can update role
Browse files Browse the repository at this point in the history
  • Loading branch information
chukitipok committed Jul 5, 2022
1 parent 6db286b commit 905526e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.sweetiez.api.adapter.delivery.role;

import fr.sweetiez.api.core.roles.CreateRoleRequest;
import fr.sweetiez.api.core.roles.RoleName;
import fr.sweetiez.api.core.roles.Role;
import fr.sweetiez.api.core.roles.RoleService;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,7 +20,7 @@ public ResponseEntity<Collection<Role>> retrieveAll() {
return ResponseEntity.ok(roleService.retrieveAll());
}

public ResponseEntity<?> create(CreateRoleRequest request) {
public ResponseEntity<?> create(RoleName request) {
try {
Role role = roleService.create(request.name());
return ResponseEntity.created(URI.create("/roles/" + role.id())).build();
Expand All @@ -29,4 +29,13 @@ public ResponseEntity<?> create(CreateRoleRequest request) {
return ResponseEntity.badRequest().build();
}
}

public ResponseEntity<Role> update(Long id, String name) {
try {
return ResponseEntity.ok(roleService.update(id , name));
}
catch (Exception exception) {
return ResponseEntity.badRequest().build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.sweetiez.api.infrastructure.repository.accounts.RoleRepository;

import java.util.Collection;
import java.util.Optional;

public class RolesAdapter implements Roles {
private final RoleRepository repository;
Expand All @@ -23,4 +24,8 @@ public Collection<Role> findAll() {
public Role save(Role role) {
return mapper.toDto(repository.save(mapper.toEntity(role)));
}

public Optional<Role> findById(Long id) {
return repository.findById(id).map(mapper::toDto);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.sweetiez.api.core.roles;

public class RoleAlreadyExistsException extends RuntimeException {}
3 changes: 3 additions & 0 deletions src/main/java/fr/sweetiez/api/core/roles/RoleName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.sweetiez.api.core.roles;

public record RoleName(String name) {}
17 changes: 17 additions & 0 deletions src/main/java/fr/sweetiez/api/core/roles/RoleService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.sweetiez.api.core.roles;

import org.springframework.http.ResponseEntity;

import java.util.Collection;

public class RoleService {
Expand All @@ -25,4 +27,19 @@ public Role create(String name) {

return roles.save(new Role(null, name));
}

public Role update(Long id, String newName) {
var roles = this.roles.findAll();

var role = roles.stream()
.filter(r -> r.id().equals(id))
.findFirst()
.orElseThrow();

if (roles.stream().anyMatch(r -> r.name().equals(newName))) {
throw new RoleAlreadyExistsException();
}

return this.roles.save(new Role(role.id(), newName));
}
}
2 changes: 2 additions & 0 deletions src/main/java/fr/sweetiez/api/core/roles/Roles.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.sweetiez.api.core.roles;

import java.util.Collection;
import java.util.Optional;

public interface Roles {
Collection<Role> findAll();
Role save(Role role);
Optional<Role> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package fr.sweetiez.api.infrastructure.delivery.roles;

import fr.sweetiez.api.adapter.delivery.role.RoleEndPoints;
import fr.sweetiez.api.core.roles.CreateRoleRequest;
import fr.sweetiez.api.core.roles.RoleName;
import fr.sweetiez.api.core.roles.Role;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

Expand All @@ -26,7 +23,12 @@ public ResponseEntity<Collection<Role>> retrieveAll() {
}

@PostMapping("/admin/roles")
private ResponseEntity<?> create(@RequestBody CreateRoleRequest request) {
return roleEndPoints.create(request);
private ResponseEntity<?> create(@RequestBody RoleName roleName) {
return roleEndPoints.create(roleName);
}

@PutMapping("/admin/roles/{id}")
private ResponseEntity<Role> update(@PathVariable Long id, @RequestBody RoleName roleName) {
return roleEndPoints.update(id, roleName.name());
}
}

0 comments on commit 905526e

Please sign in to comment.