Skip to content

Commit

Permalink
Merge pull request #25 from mahipat303/Day_4(Arup)
Browse files Browse the repository at this point in the history
day_4
  • Loading branch information
arupx3492 authored Dec 17, 2022
2 parents 5021130 + bcc569e commit b10ffd3
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.foodland.controller;

import com.foodland.exception.CategoryException;
import com.foodland.model.Category;
import com.foodland.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {
private CategoryService cs;
@PostMapping("/category/{key}")
public Category addCategory(Category category,String key) throws CategoryException {

return cs.addCategory(category,key);
}
@PutMapping("/category/{key}")
public Category updateCategory(@RequestBody Category category, String key) throws CategoryException {

return cs.updateCategory(category,key);
}

@PutMapping("/removecategory/{key}")
public Category removeCategory(@RequestBody Category category, String key) throws CategoryException {

return cs.removeCategory(category,key);
}
@GetMapping("/category/{key}")
public Category viewCategory(@RequestBody Category category, String key) throws CategoryException {

return cs.viewCategory(category,key);
}
@GetMapping("/categories/{key}")
public List<Category> viewAllCategory(String key) throws CategoryException {

return cs.viewAllCategory(key);
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.foodland.model.Restaurant;
import com.foodland.service.RestaurentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RestaurantController {
@Autowired
private RestaurentService rs;
@PostMapping("/restaurant")
public Restaurant addRestaurant( @RequestBody Restaurant restaurant){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.foodland.repository;

import com.foodland.exception.CategoryException;
import com.foodland.model.Category;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryDao extends JpaRepository<Category,Integer> {


}
10 changes: 5 additions & 5 deletions foodland/src/main/java/com/foodland/service/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

public interface CategoryService {

public Category addCategory(Category category) throws CategoryException;
public Category updateCategory(Category category) throws CategoryException;
public Category removeCategory(Category category) throws CategoryException;
public Category viewCategory(Category category) throws CategoryException;
public List<Category> viewAllCategory() throws CategoryException;
public Category addCategory(Category category,String key) throws CategoryException;
public Category updateCategory(Category category,String key) throws CategoryException;
public Category removeCategory(Category category,String key) throws CategoryException;
public Category viewCategory(Category category,String key) throws CategoryException;
public List<Category> viewAllCategory(String key) throws CategoryException;
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,121 @@
package com.foodland.serviceImpl;

import com.foodland.exception.CategoryException;
import com.foodland.exception.RestaurantException;
import com.foodland.exception.UserException;
import com.foodland.model.Category;
import com.foodland.model.CurrentUserSession;
import com.foodland.model.Restaurant;
import com.foodland.model.UserType;
import com.foodland.repository.CategoryDao;
import com.foodland.repository.SessionDao;
import com.foodland.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Optional;

public class CategoryServiseImpl implements CategoryService{
@Autowired
private SessionDao sdo;
private CategoryDao cdo;
@Override
public Category addCategory(Category category) throws CategoryException {
return null;
public Category addCategory(Category category, String key) throws CategoryException {
CurrentUserSession cus = sdo.findByUuid(key);

UserType uType = cus.getType();

if (uType.name().equals("Restaurant")) {
if(category!=null){
return cdo.save(category);
}else{
throw new CategoryException("Please Enter Valid Category");
}


} else {
throw new UserException("Please login as a Restaurant");
}
}

@Override
public Category updateCategory(Category category) throws CategoryException {
return null;
public Category updateCategory(Category category, String key) throws CategoryException {
CurrentUserSession cus = sdo.findByUuid(key);

UserType uType = cus.getType();

if (uType.name().equals("Restaurant")) {
Optional<Category> ocategory =cdo.findById(category.getCategoryId());
if(ocategory.isPresent()) {
cdo.delete(category);
return ocategory.get();
}else{
throw new CategoryException("No category Found");
}


} else {
throw new UserException("Please login as a Restaurant");
}
}

@Override
public Category removeCategory(Category category) throws CategoryException {
return null;
public Category removeCategory(Category category, String key) throws CategoryException {
CurrentUserSession cus = sdo.findByUuid(key);

UserType uType = cus.getType();

if (uType.name().equals("Restaurant")) {
Optional<Category> ocategory= cdo.findById(category.getCategoryId());

if(ocategory.isPresent()){
cdo.delete(category);
return ocategory.get();
}else{
throw new CategoryException("No Category available like :" +category);
}
} else {
throw new UserException("Please login as a Restaurant");
}
}

@Override
public Category viewCategory(Category category) throws CategoryException {
return null;
public Category viewCategory(Category category, String key) throws CategoryException {
CurrentUserSession cus = sdo.findByUuid(key);

UserType uType = cus.getType();

if (uType.name().equals("Customer") || uType.name().equals("Restaurant")) {

Optional<Category> ct=cdo.findById(category.getCategoryId());
if(ct.isPresent()) {
return ct.get();
}else{
throw new CategoryException("No Category found");
}

} else {
throw new UserException("Please Log in first");
}
}

@Override
public List<Category> viewAllCategory() throws CategoryException {
return null;
public List<Category> viewAllCategory(String key) throws CategoryException {
CurrentUserSession cus = sdo.findByUuid(key);

UserType uType = cus.getType();

if (uType.name().equals("Customer") || uType.name().equals("Restaurant")) {

List<Category> clist=cdo.findAll();
if(clist.size()==0) {
return clist;
}else{
throw new CategoryException("No Category found");
}

} else {
throw new UserException("Please Log in first");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RestaurantServiceImpl implements RestaurentService {

@Override
public Restaurant addRestaurant(Restaurant restaurant) throws RestaurantException {
return null;
return rdo.save(restaurant);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion foodland/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
server.port=8888

#db specific properties
spring.datasource.url=jdbc:mysql:https://localhost:3306/foodland
spring.datasource.url=jdbc:mysql:https://localhost:3306/db1
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Arup@MySQL
Expand Down

0 comments on commit b10ffd3

Please sign in to comment.