Skip to content

Commit

Permalink
Logger bug fixed: now it logs only successfully done operations, not …
Browse files Browse the repository at this point in the history
…all.

Ingredient admin functionality implemented.
Account email confirmation updated: provided better UX.
Refactores some duplicate code.
Refactored services to return boolean instead of null.
Bug fixed: user now can't be deleted by admin because of the logger relation, but his account can be disabled :).
  • Loading branch information
GeorgeK95 committed Apr 16, 2018
1 parent b133c8e commit 56c8ada
Show file tree
Hide file tree
Showing 114 changed files with 1,137 additions and 1,142 deletions.
3 changes: 3 additions & 0 deletions PizzaNationApp/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

893 changes: 310 additions & 583 deletions PizzaNationApp/.idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions PizzaNationApp/src/main/java/pizzaNation/PizzaNationApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import pizzaNation.app.contoller.error.ExceptionController;

/**
* Created by George-Lenovo on 13/03/2018.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pizzaNation.account.model.request.ConfirmAccountRequestModel;
import pizzaNation.app.contoller.BaseController;
import pizzaNation.user.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

import static java.util.Map.entry;
Expand Down Expand Up @@ -38,13 +39,15 @@ public ModelAndView account() {
@PreAuthorize("isAnonymous()")
@GetMapping(CONFIRM_URL)
public ModelAndView confirm(ConfirmAccountRequestModel model) {
return super.view(model, Map.ofEntries(entry(PAGE_TITLE_STR, CONFOIRM_PAGE_TITLE)));
return super.view(model, Map.ofEntries(entry(PAGE_TITLE_STR, CONFIRM_PAGE_TITLE)));
}

@PreAuthorize("isAnonymous()")
@PostMapping(CONFIRM_URL)
public ModelAndView confirmProcess(ConfirmAccountRequestModel model, RedirectAttributes attributes) {
this.userService.confirmAccount(model.getToken(), attributes);
if (!this.userService.confirmAccount(model.getToken(), attributes))
return super.redirect(CONFIRM_URL);
return super.redirect(LOGIN_URL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
@Controller
@RequestMapping(ADMIN_URL)
public class AdminController extends BaseController {
static final Map<String, Object> ADMIN_PAGE_TITLE_MAP_ENTRY = Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE));

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(EMPTY_URL)
public ModelAndView admin() {
return super.view(null, Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(null, ADMIN_PAGE_TITLE_MAP_ENTRY);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pizzaNation.app.annotation.LoggerAction;
import pizzaNation.app.contoller.BaseController;
import pizzaNation.app.enums.Action;
import pizzaNation.app.enums.TableEnum;
import pizzaNation.app.enums.Unit;
import pizzaNation.app.model.request.EditIngredientRequestModel;
import pizzaNation.app.model.request.IngredientsRequestModelWrapper;
import pizzaNation.app.service.contract.IIngredientService;

Expand All @@ -18,6 +22,7 @@
import java.util.Map;

import static java.util.Map.entry;
import static pizzaNation.admin.controller.AdminController.ADMIN_PAGE_TITLE_MAP_ENTRY;
import static pizzaNation.app.util.WebConstants.*;

/**
Expand All @@ -42,47 +47,59 @@ public List<Unit> getProducts() {
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@RequestMapping(method = RequestMethod.GET, value = {ALL_INGREDIENTS_URL, INGREDIENTS_URL})
public ModelAndView allIngredients() {
return super.view(this.ingredientService.findAllByDateDesc(), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.ingredientService.findAllByDateDesc(), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(ADD_PRODUCT_INGREDIENTS_URL)
public ModelAndView addIngredients(@PathVariable String productName) {
return super.view(this.ingredientService.getRequestModels(), Map.ofEntries(
entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE),
ADMIN_PAGE_TITLE_MAP_ENTRY.entrySet().stream().findFirst().get(),
entry(PRODUCT_NAME_STR, productName)
));
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(ADD_PRODUCT_INGREDIENTS_URL)
@LoggerAction(table = TableEnum.INGREDIENT, action = Action.ADD)
public ModelAndView addIngredientsProcess(@PathVariable String productName,
@ModelAttribute @Valid IngredientsRequestModelWrapper model,
BindingResult bindingResult,
RedirectAttributes attributes) {
if (this.ingredientService.addIngredientsAndSetThemToProduct(productName, model, bindingResult, attributes))
if (!this.ingredientService.addIngredientsAndSetThemToProduct(productName, model, bindingResult, attributes))
return super.redirect(ADMIN_SET_PRODUCT_INGREDIENTS_URL);
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
return super.redirectAndLog(ADMIN_ALL_PRODUCTS_URL);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(DELETE_PRODUCT_INGREDIENTS_URL)
public ModelAndView deleteIngredients(@PathVariable String productName) {
return super.view(this.ingredientService.getRequestModels(), Map.ofEntries(
entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE),
entry(PRODUCT_NAME_STR, productName)
));
@GetMapping(EDIT_INGREDIENTS_URL)
public ModelAndView editIngredient(@PathVariable String id) {
return super.view(this.ingredientService.findOne(id), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_PRODUCT_INGREDIENTS_URL)
public ModelAndView deleteIngredientsProcess(@PathVariable String productName,
@ModelAttribute @Valid IngredientsRequestModelWrapper model,
BindingResult bindingResult,
RedirectAttributes attributes) {
if (this.ingredientService.addIngredientsAndSetThemToProduct(productName, model, bindingResult, attributes))
return super.redirect(ADMIN_SET_PRODUCT_INGREDIENTS_URL);
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
@PostMapping(EDIT_INGREDIENTS_URL)
@LoggerAction(table = TableEnum.INGREDIENT, action = Action.EDIT)
public ModelAndView editIngredientProcess(@ModelAttribute @Valid EditIngredientRequestModel editIngredientRequestModel,
BindingResult bindingResult, @PathVariable String id, RedirectAttributes attributes) {
if (!this.ingredientService.editIngredient(editIngredientRequestModel, attributes, bindingResult, id))
return super.redirect(ADMIN_EDIT_INGREDIENTS_URL.concat(SLASH_STR.concat(id)));
return super.redirectAndLog(ADMIN_ALL_INGREDIENTS_URL);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(DELETE_INGREDIENTS_URL)
public ModelAndView deleteIngredient(@PathVariable String id) {
return super.view(this.ingredientService.findById(id), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_INGREDIENTS_URL)
@LoggerAction(table = TableEnum.INGREDIENT, action = Action.DELETE)
public ModelAndView deleteIngredientProcess(@PathVariable String id, RedirectAttributes attributes) {
if (!this.ingredientService.deleteIngredient(id, attributes))
return super.redirect(DELETE_INGREDIENTS_URL.concat(SLASH_STR.concat(id)));
return super.redirectAndLog(ADMIN_ALL_INGREDIENTS_URL);
}

/*@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
Expand All @@ -100,34 +117,22 @@ public ModelAndView addIngredientProcess(@ModelAttribute @Valid AddIngredientReq
return super.redirect(ADMIN_ADD_INGREDIENTS_URL);
return super.redirect(ADMIN_ALL_INGREDIENTS_URL);
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(EDIT_INGREDIENTS_URL)
public ModelAndView editIngredient(@PathVariable String name) {
return super.view(this.ingredientService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(EDIT_INGREDIENTS_URL)
@LoggerAction(table = TableEnum.INGREDIENT, action = Action.EDIT)
public ModelAndView editIngredientProcess(@ModelAttribute @Valid EditIngredientRequestModel editIngredientRequestModel,
BindingResult bindingResult, @PathVariable String name, RedirectAttributes attributes) {
if (!this.ingredientService.editIngredient(editIngredientRequestModel, attributes, bindingResult, name))
return super.redirect(ADMIN_EDIT_INGREDIENTS_URL.concat(SLASH_STR + name));
return super.redirect(ADMIN_ALL_INGREDIENTS_URL);
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(DELETE_INGREDIENTS_URL)
public ModelAndView deleteIngredient(@PathVariable String name) {
return super.view(this.ingredientService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
@GetMapping(DELETE_PRODUCT_INGREDIENTS_URL)
public ModelAndView deleteIngredients(@PathVariable String productName) {
return super.view(this.ingredientService.findProductIngredients(productName), Map.ofEntries(
entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE),
entry(PRODUCT_NAME_STR, productName)
));
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_INGREDIENTS_URL)
@LoggerAction(table = TableEnum.INGREDIENT, action = Action.DELETE)
public ModelAndView deleteIngredientProcess(@PathVariable String name) {
this.ingredientService.deleteIngredient(name);
return super.redirect(ADMIN_ALL_INGREDIENTS_URL);
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_PRODUCT_INGREDIENTS_URL)
public ModelAndView deleteIngredientsProcess(@PathVariable String productName,
@ModelAttribute @Valid IngredientsRequestModelWrapper model,
BindingResult bindingResult,
RedirectAttributes attributes) {
if (this.ingredientService.addIngredientsAndSetThemToProduct(productName, model, bindingResult, attributes))
return super.redirect(ADMIN_SET_PRODUCT_INGREDIENTS_URL);
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pizzaNation.admin.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import pizzaNation.app.contoller.BaseController;

import static pizzaNation.admin.controller.AdminController.ADMIN_PAGE_TITLE_MAP_ENTRY;
import static pizzaNation.app.util.WebConstants.ADMIN_LOGS_ALL_URL;

/**
* Created by George-Lenovo on 17/04/2018.
*/
@Controller
public class AdminLogController extends BaseController {

@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
@GetMapping(ADMIN_LOGS_ALL_URL)
public ModelAndView allLogs() {
return super.view(null, ADMIN_PAGE_TITLE_MAP_ENTRY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;

import static java.util.Map.entry;
import static pizzaNation.admin.controller.AdminController.ADMIN_PAGE_TITLE_MAP_ENTRY;
import static pizzaNation.app.util.WebConstants.*;
import static pizzaNation.app.util.WebConstants.ADMIN_ALL_MENUS_URL;
import static pizzaNation.app.util.WebConstants.ADMIN_EDIT_MENUS_URL;
Expand Down Expand Up @@ -61,13 +62,13 @@ public List<MenuViewModel> getMenus() {
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@RequestMapping(method = RequestMethod.GET, value = {ALL_MENUS_URL, MENUS_URL})
public ModelAndView allMenus() {
return super.view(this.menuService.findAllByDateDesc(), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.menuService.findAllByDateDesc(), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(ADD_MENUS_URL)
public ModelAndView addMenu(AddMenuRequestModel addMenuRequestModel) {
return super.view(addMenuRequestModel, Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(addMenuRequestModel, ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
Expand All @@ -77,14 +78,14 @@ public ModelAndView addMenuProcess(@ModelAttribute @Valid AddMenuRequestModel ad
RedirectAttributes attributes) {
if (!this.menuService.addMenu(addMenuRequestModel, attributes, bindingResult))
return super.redirect(ADMIN_ADD_MENUS_URL);
return super.redirect(ADMIN_ALL_MENUS_URL);
return super.redirectAndLog(ADMIN_ALL_MENUS_URL);
}


@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(EDIT_MENUS_URL)
public ModelAndView editMenu(@PathVariable String name) {
return super.view(this.menuService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.menuService.findByName(name), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
Expand All @@ -94,20 +95,20 @@ public ModelAndView editMenuProcess(@ModelAttribute @Valid EditMenuRequestModel
@PathVariable String name, RedirectAttributes attributes) {
if (!this.menuService.editMenu(editMenuRequestModel, attributes, bindingResult, name))
return super.redirect(ADMIN_EDIT_MENUS_URL.concat(SLASH_STR + name));
return super.redirect(ADMIN_ALL_MENUS_URL);
return super.redirectAndLog(ADMIN_ALL_MENUS_URL);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(DELETE_MENUS_URL)
public ModelAndView deleteMenu(@PathVariable String name) {
return super.view(this.menuService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.menuService.findByName(name), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_MENUS_URL)
@LoggerAction(table = TableEnum.MENU, action = Action.DELETE)
public ModelAndView deleteMenuProcess(@PathVariable String name) {
this.menuService.deleteMenu(name);
return super.redirect(ADMIN_ALL_MENUS_URL);
return super.redirectAndLog(ADMIN_ALL_MENUS_URL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.util.Map.entry;
import static pizzaNation.admin.controller.AdminController.ADMIN_PAGE_TITLE_MAP_ENTRY;
import static pizzaNation.app.util.WebConstants.*;

/**
Expand Down Expand Up @@ -55,13 +54,13 @@ public List<Unit> getUnits() {
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@RequestMapping(method = RequestMethod.GET, value = {ALL_PRODUCTS_URL, PRODUCTS_URL})
public ModelAndView allProducts() {
return super.view(this.productService.findAllByDate(), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.productService.findAllByDate(), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(ADD_PRODUCTS_URL)
public ModelAndView addProduct(AddProductRequestModel addProductRequestModel) {
return super.view(addProductRequestModel, Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(addProductRequestModel, ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
Expand All @@ -71,14 +70,14 @@ public ModelAndView addProductProcess(@ModelAttribute @Valid AddProductRequestMo
BindingResult bindingResult, RedirectAttributes attributes) {
if (!this.productService.addProduct(addProductRequestModel, attributes, bindingResult))
return super.redirect(ADMIN_ADD_PRODUCTS_URL);
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
return super.redirectAndLog(ADMIN_ALL_PRODUCTS_URL);
}


@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(EDIT_PRODUCTS_URL)
public ModelAndView editProduct(@PathVariable String name) {
return super.view(this.productService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.productService.findByName(name), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
Expand All @@ -88,21 +87,22 @@ public ModelAndView editProductProcess(@ModelAttribute @Valid EditProductRequest
BindingResult bindingResult, @PathVariable String name, RedirectAttributes attributes) {
if (!this.productService.editProduct(editProductRequestModel, attributes, bindingResult, name))
return super.redirect(ADMIN_EDIT_PRODUCTS_URL.concat(SLASH_STR + name));
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
return super.redirectAndLog(ADMIN_ALL_PRODUCTS_URL);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@GetMapping(DELETE_PRODUCTS_URL)
public ModelAndView deleteProduct(@PathVariable String name) {
return super.view(this.productService.findByName(name), Map.ofEntries(entry(PAGE_TITLE_STR, ADMIN_PANEL_PAGE_TITLE)));
return super.view(this.productService.findByName(name), ADMIN_PAGE_TITLE_MAP_ENTRY);
}

@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')")
@PostMapping(DELETE_PRODUCTS_URL)
@LoggerAction(table = TableEnum.PRODUCT, action = Action.DELETE)
public ModelAndView deleteProductProcess(@PathVariable String name) {
this.productService.deleteProduct(name);
return super.redirect(ADMIN_ALL_PRODUCTS_URL);
if (this.productService.deleteProduct(name))
return super.redirectAndLog(DELETE_PRODUCTS_URL.concat(name));
return super.redirectAndLog(ADMIN_ALL_PRODUCTS_URL);
}

}
Loading

0 comments on commit 56c8ada

Please sign in to comment.