Changeset f7b963f


Ignore:
Timestamp:
01/08/23 23:00:21 (2 years ago)
Author:
Bojan <bojantrpeski123@…>
Branches:
main
Children:
5168b2e
Parents:
7225443
Message:

Add article to category implemented

Location:
FullyStocked/src/main
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/ArticlesRepository.java

    r7225443 rf7b963f  
    1111    List<Articles> findAllByArticlenameContainingIgnoreCase(String name);
    1212    List<Articles> findAllByCategoryListContaining(Categories category);
     13    List<Articles> findAllBy();
    1314}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/CategoriesRepository.java

    r7225443 rf7b963f  
    99public interface CategoriesRepository extends JpaRepository<Categories, Integer> {
    1010    List<Categories> findAllByCategorynameContainsIgnoreCase(String name);
     11    List<Categories> findAll();
    1112}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/ArticlesService.java

    r7225443 rf7b963f  
    1515    Optional<Articles> addToCategory(Integer articleId, Integer categoryId);
    1616    List<Categories> findAllCategoriesByArticle(Integer articleId);
     17    List<Articles> findAll();
    1718
    1819    List<SupplierSuppliesArticleReport> findAllBySupplier(Integer id);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/ArticlesServiceImpl.java

    r7225443 rf7b963f  
    5656        Categories category=categoriesRepository.findById(categoryId).orElseThrow(InvalidArgumentsException::new);
    5757        Articles articles=articlesRepository.findById(articleId).orElseThrow(InvalidArgumentsException::new);
     58        if(articles.getCategoryList().contains(category))
     59        {
     60            return Optional.of(articles);
     61        }
    5862        articles.getCategoryList().add(category);
    5963        articlesRepository.save(articles);
     
    6872
    6973    @Override
     74    public List<Articles> findAll() {
     75        return articlesRepository.findAll();
     76    }
     77
     78    @Override
    7079    public List<SupplierSuppliesArticleReport> findAllBySupplier(Integer supplierId) {
    7180        return supplierSuppliesArticleRepository.findAllById_Userid(supplierId);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Web/Controller/ArticleController.java

    r7225443 rf7b963f  
    11package com.bazi.fullystocked.Web.Controller;
    22
     3import com.bazi.fullystocked.Models.Articles;
     4import com.bazi.fullystocked.Models.Categories;
    35import com.bazi.fullystocked.Models.Exceptions.InvalidArgumentsException;
    46import com.bazi.fullystocked.Services.ArticlesService;
     7import com.bazi.fullystocked.Services.CategoriesService;
    58import org.springframework.stereotype.Controller;
    69import org.springframework.ui.Model;
     
    1114
    1215import javax.servlet.http.HttpServletRequest;
     16import java.util.List;
    1317
    1418@Controller
     
    1620public class ArticleController {
    1721    private final ArticlesService articlesService;
     22    private final CategoriesService categoriesService;
    1823
    19     public ArticleController(ArticlesService articlesService) {
     24    public ArticleController(ArticlesService articlesService, CategoriesService categoriesService) {
    2025        this.articlesService = articlesService;
     26        this.categoriesService = categoriesService;
    2127    }
    2228
     
    2733    }
    2834    @PostMapping("/create")
    29     public String createInvoice(@RequestParam String aname,
     35    public String createArticle(@RequestParam String aname,
    3036                                @RequestParam String opis,
    3137                                @RequestParam(required = false) String slika,
     
    4450
    4551        }
    46         return "redirect:/homeManager";
     52        return "redirect:/manager";
     53    }
     54    @GetMapping("/category/add")
     55    private String categoryArticle(Model m)
     56    {
     57        List<Articles> articlesList=articlesService.findAll();
     58        List<Categories> categories=categoriesService.findAll();
     59        m.addAttribute("articles",articlesList);
     60        m.addAttribute("categories",categories);
     61
     62
     63        return "addArticleCategory";
     64    }
     65    @PostMapping("/category/add")
     66    private String categoryArticle(@RequestParam Integer article,
     67                                   @RequestParam Integer category)
     68    {
     69    try{
     70        articlesService.addToCategory(article,category);
     71    }
     72    catch (InvalidArgumentsException e)
     73    {
     74        return "redirect:/articles/category/add?error="+e.getMessage();
     75    }
     76
     77
     78        return "redirect:/manager";
    4779    }
    4880}
Note: See TracChangeset for help on using the changeset viewer.