Skip to content

Commit

Permalink
New messages created for types and categories api response.
Browse files Browse the repository at this point in the history
  • Loading branch information
pillowslept committed Jun 10, 2019
1 parent f427736 commit e8c3b17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/co/com/expenses/service/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String create(Params params) {

private void validateCreate(Params params) {
if(Validations.field(params.getDescription())){
throw new ValidateException(String.format(messages.get("category.field.required"), "description"));
throw new ValidateException(String.format(messages.get("default.field.required"), "description"));
}
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public Category validateAndFind(Long id) {

private void validateId(Long id) {
if(Validations.field(id)){
throw new ValidateException(String.format(messages.get("category.field.required"), "categoryId"));
throw new ValidateException(String.format(messages.get("default.field.required"), "categoryId"));
}
}

Expand Down
26 changes: 11 additions & 15 deletions src/main/java/co/com/expenses/service/TypeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import co.com.expenses.component.Messages;
import co.com.expenses.dto.Params;
import co.com.expenses.dto.Util;
import co.com.expenses.enums.State;
Expand All @@ -19,17 +20,12 @@
@Transactional
public class TypeService {

private static final String TYPE_CREATED = "El tipo de movimiento ha sido creado con éxito, el identificador generado es <%d>";
private static final String TYPE_NOT_FOUND = "El tipo de movimiento con identificador <%d> no existe en la base de datos";
private static final String TYPE_ACTIVATED = "El tipo de movimiento con identificador <%d> fue activado con éxito";
private static final String TYPE_INACTIVATED = "El tipo de movimiento con identificador <%d> fue inactivado con éxito";
private static final String DESCRIPTION_NOT_VALID = "El campo <description> no es válido";
private static final String TYPE_ID_NOT_VALID = "El campo <typeId> no es válido";
private static final String TYPE_EXISTS = "El tipo de movimiento con descripción <%s> ya existe en la base de datos";

@Autowired
TypeRepository typeRepository;

@Autowired
Messages messages;

public Type findById(Long id) {
return typeRepository.findOne(id);
}
Expand All @@ -41,19 +37,19 @@ public String create(Params params) {
.state(State.ACTIVE.get())
.build();
typeRepository.save(type);
return String.format(TYPE_CREATED, type.getId());
return String.format(messages.get("type.created"), type.getId());
}

private void validateExistence(String description) {
Type type = typeRepository.findByDescriptionIgnoreCase(description);
if(type != null) {
throw new ValidateException(String.format(TYPE_EXISTS, description));
throw new ValidateException(String.format(messages.get("type.exists"), description));
}
}

private void validateCreate(Params params) {
if(Validations.field(params.getDescription())){
throw new ValidateException(DESCRIPTION_NOT_VALID);
throw new ValidateException(String.format(messages.get("default.field.required"), "description"));
}
validateExistence(params.getDescription());
}
Expand All @@ -62,28 +58,28 @@ public String inactivate(Params params) {
Type type = validateAndFind(params.getTypeId());
type.setState(State.INACTIVE.get());
update(type);
return String.format(TYPE_INACTIVATED, params.getTypeId());
return String.format(messages.get("type.inactivated"), params.getTypeId());
}

public String activate(Params params) {
Type type = validateAndFind(params.getTypeId());
type.setState(State.ACTIVE.get());
update(type);
return String.format(TYPE_ACTIVATED, params.getTypeId());
return String.format(messages.get("type.inactivated"), params.getTypeId());
}

public Type validateAndFind(Long id) {
validateId(id);
Type type = findById(id);
if(type == null){
throw new ValidateException(String.format(TYPE_NOT_FOUND, id));
throw new ValidateException(String.format(messages.get("type.not.found"), id));
}
return type;
}

private void validateId(Long id) {
if(Validations.field(id)){
throw new ValidateException(TYPE_ID_NOT_VALID);
throw new ValidateException(String.format(messages.get("default.field.required"), "typeId"));
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ category.created = El tipo de categoría ha sido creado con éxito, el identific
category.not.found = El tipo de categoría con identificador <%d> no existe en la base de datos
category.activated = El tipo de categoría con identificador <%d> fue activado con éxito
category.inactivated = El tipo de categoría con identificador <%d> fue inactivado con éxito
category.field.required = El campo <%s> no es válido
## TYPES MESSAGES
type.activated = El tipo de movimiento con identificador <%d> fue inactivado con éxito
type.created = El tipo de movimiento ha sido creado con éxito, el identificador generado es <%d>
type.exists = El tipo de movimiento con descripción <%s> ya existe en la base de datos
type.inactivated = El tipo de movimiento con identificador <%d> fue inactivado con éxito
type.not.found = El tipo de movimiento con identificador <%d> no existe en la base de datos
## DEFAULT VALIDATIONS
default.field.required = El campo <%s> no es válido
## COMMON MESSAGES
default.username = Juan Camilo Velásquez
## DATE MESSAGES
Expand Down

0 comments on commit e8c3b17

Please sign in to comment.