Created
August 31, 2021 06:07
-
-
Save giridhar30/877f5850c5d1d2413b3749ec569df885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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