| | 18 | ** При испраќање на апликацијата преку POST барање најпрво преку jwt токенот се пребарува мејлот на корисникот. Потоа се прават некои основни проверки пред да се додаде апликацијата во базата ** |
| | 19 | |
| | 20 | {{{ |
| | 21 | @PostMapping("/recipes/add") |
| | 22 | public ResponseEntity<?> addRecipeApplication( |
| | 23 | @RequestHeader("Authorization") String tokenHeader, |
| | 24 | @RequestBody RecipeApplicationDTO recipeApplicationDTO |
| | 25 | ) { |
| | 26 | String token = tokenHeader.substring(7); |
| | 27 | String email = jwtUtil.extractEmail(token); |
| | 28 | |
| | 29 | if (email == null) { |
| | 30 | return new ResponseEntity<>("Error finding user", HttpStatus.BAD_REQUEST); |
| | 31 | } |
| | 32 | |
| | 33 | if (recipeApplicationDTO.getRecipeName() == null || recipeApplicationDTO.getRecipeName().isEmpty()) { |
| | 34 | return new ResponseEntity<>("Recipe name is required", HttpStatus.BAD_REQUEST); |
| | 35 | } |
| | 36 | |
| | 37 | if (recipeApplicationDTO.getIngredients() == null || recipeApplicationDTO.getIngredients().isEmpty()) { |
| | 38 | return new ResponseEntity<>("Ingredients are required", HttpStatus.BAD_REQUEST); |
| | 39 | } |
| | 40 | |
| | 41 | recipeService.addRecipeApplication(recipeApplicationDTO); |
| | 42 | return new ResponseEntity<>(HttpStatus.OK); |
| | 43 | } |
| | 44 | }}} |
| | 45 | |
| | 46 | ** Во сервисниот дел апликацијата само се пренесува до последниот слој. ** |
| | 47 | |
| | 48 | {{{ |
| | 49 | @Override |
| | 50 | public void addRecipeApplication(RecipeApplicationDTO recipeApplicationDTO) { |
| | 51 | recipeDAO.addRecipeApplication(recipeApplicationDTO); |
| | 52 | } |
| | 53 | }}} |
| | 54 | |
| | 55 | ** Во последниот слој се додава потребнит ** |
| | 56 | |
| | 57 | {{{ |
| | 58 | @Override |
| | 59 | public void addRecipeApplication(RecipeApplicationDTO recipeApplicationDTO) { |
| | 60 | String sql = "INSERT INTO recipe_application " + |
| | 61 | "(recipe_name, description, category, origin, meal_thumb, video_url) " + |
| | 62 | "VALUES (?, ?, ?, ?, ?, ?) RETURNING id"; |
| | 63 | |
| | 64 | Integer recipeId = jdbcTemplate.queryForObject(sql, new Object[] { |
| | 65 | recipeApplicationDTO.getRecipeName(), |
| | 66 | recipeApplicationDTO.getRecipeDesc(), |
| | 67 | recipeApplicationDTO.getRecipeCategory(), |
| | 68 | recipeApplicationDTO.getRecipeOrigin(), |
| | 69 | recipeApplicationDTO.getRecipeMealThumb(), |
| | 70 | recipeApplicationDTO.getRecipeVideoURL() |
| | 71 | }, Integer.class); |
| | 72 | |
| | 73 | if (recipeId == null) { |
| | 74 | throw new IllegalStateException("Failed to retrieve generated id for the recipe."); |
| | 75 | } |
| | 76 | |
| | 77 | recipeApplicationDTO.getIngredients().forEach(ingredient -> { |
| | 78 | jdbcTemplate.update("INSERT INTO recipe_application_ingredients (ingredient, dose, recipe_id) VALUES (?, ?, ?)", ingredient.getIngredient(), ingredient.getDose(), recipeId); |
| | 79 | }); |
| | 80 | } |
| | 81 | }}} |
| | 82 | |