@Override public Category update(Long id, String name) { Category category = get(id); if (repository.findByName(name) != null) { throw new EntityAlreadyExistsException( String.format("There already exists a category with name=%s!", name)); } category.setName(name); repository.save(category); return category; }
@Override public Category create(Category category) { if (category.getId() != null && repository.findOne(category.getId()) != null) { throw new EntityAlreadyExistsException( String.format("There already exists a category with id=%d!", category.getId())); } else if (repository.findByName(category.getName()) != null) { throw new EntityAlreadyExistsException( String.format("There already exists a category with name=%s!", category.getName())); } else { repository.save(category); } return category; }
@Override public Category get(Long id) { Category category = repository.findOne(id); if (category == null) { throw new EntityNotFoundException(String.format("No category with id=%d exists!", id)); } else { return category; } }
@Override public Page<Category> findAll(Integer pageNumber, Integer pageSize) { return repository.findAll(new PageRequest(pageNumber, pageSize)); }
@Override public Category delete(Long id) { Category category = get(id); repository.delete(category); return category; }