Skip to content

Commit

Permalink
feat: RoleRequestDTO & RoleService refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
HunorTotBagi committed Aug 6, 2024
1 parent 44c64d9 commit 2d7cd23
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/electric_diary/DTO/Request/RoleRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.electric_diary.DTO.Request;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class RoleRequestDTO {
private String name;
}
17 changes: 9 additions & 8 deletions src/main/java/com/electric_diary/controllers/RoleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.electric_diary.DTO.Request.RoleRequestDTO;
import com.electric_diary.entities.RoleEntity;
import com.electric_diary.services.RoleService;

Expand All @@ -23,8 +24,8 @@ public class RoleController {
protected RoleService roleService;

@PostMapping
public ResponseEntity<RoleEntity> createParent(@RequestBody RoleEntity roleBody) {
return new ResponseEntity<RoleEntity>(roleService.createRole(roleBody), HttpStatus.OK);
public ResponseEntity<RoleEntity> createParent(@RequestBody RoleRequestDTO roleRequestDTO) {
return new ResponseEntity<RoleEntity>(roleService.createRole(roleRequestDTO), HttpStatus.OK);
}

@GetMapping
Expand All @@ -33,17 +34,17 @@ public ResponseEntity<Iterable<RoleEntity>> getAllRoles() {
}

@GetMapping("/{id}")
public ResponseEntity<RoleEntity> getRoleById(@PathVariable String id) {
return new ResponseEntity<>(roleService.getRoleById(id), HttpStatus.OK);
public ResponseEntity<RoleEntity> getRoleById(@PathVariable Integer roleId) {
return new ResponseEntity<>(roleService.getRoleById(roleId), HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<RoleEntity> updateRole(@PathVariable String id, @RequestBody RoleEntity roleBody) {
return new ResponseEntity<>(roleService.updateRole(id, roleBody), HttpStatus.OK);
public ResponseEntity<RoleEntity> updateRole(@PathVariable Integer roleId, @RequestBody RoleRequestDTO roleRequestDTO) {
return new ResponseEntity<>(roleService.updateRole(roleId, roleRequestDTO), HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<RoleEntity> deleteRole(@PathVariable String id) {
return new ResponseEntity<>(roleService.deleteRole(id), HttpStatus.OK);
public ResponseEntity<RoleEntity> deleteRole(@PathVariable Integer roleId) {
return new ResponseEntity<>(roleService.deleteRole(roleId), HttpStatus.OK);
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/electric_diary/services/RoleService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.electric_diary.services;

import com.electric_diary.DTO.Request.RoleRequestDTO;
import com.electric_diary.entities.RoleEntity;

public interface RoleService {
public RoleEntity createRole(RoleEntity roleBody);
public RoleEntity createRole(RoleRequestDTO roleRequestDTO);

public Iterable<RoleEntity> getAllRoles();

public RoleEntity getRoleById(String id);
public RoleEntity getRoleById(Integer roleId);

public RoleEntity updateRole(String id, RoleEntity roleBody);
public RoleEntity updateRole(Integer roleId, RoleRequestDTO roleRequestDTO);

public RoleEntity deleteRole(String id);
public RoleEntity deleteRole(Integer roleId);
}
50 changes: 10 additions & 40 deletions src/main/java/com/electric_diary/services/impl/RoleServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.electric_diary.services.impl;

import java.util.Optional;

import org.springframework.stereotype.Service;

import com.electric_diary.DTO.Request.RoleRequestDTO;
import com.electric_diary.entities.RoleEntity;
import com.electric_diary.exception.NotFoundException;
import com.electric_diary.repositories.RoleRepository;
Expand All @@ -24,9 +23,9 @@ public RoleServiceImpl(final RoleRepository roleRepository) {
}

@Override
public RoleEntity createRole(RoleEntity roleBody) {
public RoleEntity createRole(RoleRequestDTO roleRequestDTO) {
RoleEntity role = new RoleEntity();
role.setName(roleBody.getName());
role.setName(roleRequestDTO.getName());
roleRepository.save(role);
return role;
}
Expand All @@ -37,50 +36,21 @@ public Iterable<RoleEntity> getAllRoles() {
}

@Override
public RoleEntity getRoleById(String id) {
try {
int roleId = Integer.parseInt(id);
RoleEntity role = roleRepository.findById(roleId).orElseThrow(() -> new NotFoundException("Role", id));
return role;
} catch (NumberFormatException e) {
throw new NumberFormatException();
}
public RoleEntity getRoleById(Integer roleId) {
return roleRepository.findById(roleId).orElseThrow(() -> new NotFoundException("Role", roleId));
}

@Override
public RoleEntity updateRole(String id, RoleEntity roleBody) {
int roleId;
try {
roleId = Integer.parseInt(id);
} catch (NumberFormatException e) {
throw new NumberFormatException();
}

Optional<RoleEntity> optionalRole = roleRepository.findById(roleId);
if (!optionalRole.isPresent())
throw new NotFoundException("Role", id);

RoleEntity role = optionalRole.get();
role.setName(roleBody.getName());
public RoleEntity updateRole(Integer roleId, RoleRequestDTO roleRequestDTO) {
RoleEntity role = getRoleById(roleId);
role.setName(roleRequestDTO.getName());
roleRepository.save(role);
return role;
}

@Override
public RoleEntity deleteRole(String id) {
int roleId;
try {
roleId = Integer.parseInt(id);
} catch (NumberFormatException e) {
throw new NumberFormatException();
}

Optional<RoleEntity> optionalRole = roleRepository.findById(roleId);

if (!optionalRole.isPresent())
throw new NotFoundException("Role", id);

RoleEntity role = optionalRole.get();
public RoleEntity deleteRole(Integer roleId) {
RoleEntity role = getRoleById(roleId);
roleRepository.delete(role);
return role;
}
Expand Down

0 comments on commit 2d7cd23

Please sign in to comment.