API Docs:
https://docs.spring.io/spring-data/data-jpa/docs/current/api/
Ref docs:
https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#reference
Spring Java Docs:
Link: https://docs.spring.io/spring-framework/docs/current/javadoc-api/
Solve
List all veg Food Items
Add a method in FoodItemDao extends JpaRepository<FoodItem, Long>
:
List<FoodItem> findByIsVegTrue();
Get restaurant and food items details
Input: Restaurant ID
Add a method in RestaurantDao extends JpaRepository<Restaurant, Long>
:
@Query("select r from Restaurant r left join fetch r.foodItems where r.id = :restaurantId")
Optional<Restaurant> getCompleDetails(Long restaurantId);
Update price and description of a food item by its name and restaurant id
Input: Food item name and restaurant ID (unique), new price, and new description
3.1
Optional<FoodItem> findByItemNameAndMyRestaurantId(String name, Long restaurantId);
3.2 Invoke it from service layer → orElseThrow
→ call setters → upon service layer method returns → tx.commit
→ update (DML) → controller.
OR
@Query("update FoodItem f set f.price=:pr , f.description=:desc where f.ItemName=:nm and f.myRestaurant.id=:id")
@Modifying
int | long | void updateFoodItemDetails(double pr, String desc, String nm, Long id);
Delete product by its name
Input: Product name (unique)
DAO: ProductDao
int deleteByName(String nm);
List all products having price less than specified price and from specified category (by category name)
DAO: ProductDao
List<Product> findByPriceLessThanAndMyCategoryName(double maxPrice, String catName);
Apply specified % discount on all products from specified category id, which have not been updated recently
Input: Discount in %, category ID, number of days (not purchased)
In ProductServiceImpl
:
LocalDateTime requiredDateTime = LocalDateTime.now().minusDays(noOfDays);
Call DAO’s method:
@Query("update Product p set p.price=p.price-(p.price*:disc/100) where p.myCategory.id=:catId and p.updatedOn < :oldDateTime")
@Modifying
int applyDiscount(double disc, Long catId, LocalDateTime oldDateTime);
1.1 Back end development of full stack application
1.2 Annotations
@ResponseBody