Skip to content

Instantly share code, notes, and snippets.

@giridhar30
Created August 31, 2021 06:07
Show Gist options
  • Save giridhar30/877f5850c5d1d2413b3749ec569df885 to your computer and use it in GitHub Desktop.
Save giridhar30/877f5850c5d1d2413b3749ec569df885 to your computer and use it in GitHub Desktop.
package com.educative.ecommerce.service;
import com.educative.ecommerce.model.Category;
import com.educative.ecommerce.repository.Categoryrepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class CategoryService {
@Autowired
private Categoryrepository categoryrepository;
public List<Category> listCategories() {
return categoryrepository.findAll();
}
public void createCategory(Category category) {
categoryrepository.save(category);
}
public Category readCategory(String categoryName) {
return categoryrepository.findByCategoryName(categoryName);
}
public Optional<Category> readCategory(Integer categoryId) {
return categoryrepository.findById(categoryId);
}
public void updateCategory(Integer categoryID, Category newCategory) {
Category category = categoryrepository.findById(categoryID).get();
category.setCategoryName(newCategory.getCategoryName());
category.setDescription(newCategory.getDescription());
category.setImageUrl(newCategory.getImageUrl());
categoryrepository.save(category);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment