Changeset 0afa35a


Ignore:
Timestamp:
01/08/23 20:04:25 (23 months ago)
Author:
DarkoSasanski <darko.sasanski@…>
Branches:
main
Children:
4a93293
Parents:
16f38d4
Message:

Implemented question and answers logic

Location:
FullyStocked/src/main
Files:
10 added
8 edited

Legend:

Unmodified
Added
Removed
  • FullyStocked/src/main/java/com/bazi/fullystocked/Models/Answers.java

    r16f38d4 r0afa35a  
    88import javax.validation.constraints.NotEmpty;
    99import javax.validation.constraints.NotNull;
     10import java.io.Serializable;
    1011import java.time.LocalDateTime;
    1112
     
    2223    private String answertext;
    2324
    24     public Answers(Questions question, String answertext) {
     25    public Answers(Questions question, Integer answerCount, String answertext) {
    2526        this.datecreated = LocalDateTime.now();
    2627        this.answertext = answertext;
    27         answerId.setQuestion(question);
     28        this.answerId=new AnswerId();
     29        this.answerId.setAnswerid(answerCount);
     30        this.answerId.setQuestion(question);
    2831    }
    2932}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Models/Enumerations/ArticleStatus.java

    r16f38d4 r0afa35a  
    22
    33public enum ArticleStatus {
    4     ORDERED, DELIVERED, PROCESSED
     4    ORDERED("Ordered"), DELIVERED("Delivered"), PROCESSED("Processed");
     5    private String name;
     6
     7    ArticleStatus(String s) {
     8        this.name=s;
     9    }
     10    public String getName() {
     11        return this.name;
     12    }
     13    public String toString() {
     14        return name;
     15    }
    516}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Models/Enumerations/OrderPriority.java

    r16f38d4 r0afa35a  
    22
    33public enum OrderPriority {
    4     LOW, MEDIUM, HIGH;
     4    LOW("Low"), MEDIUM("Medium"), HIGH("High");
     5    private String name;
     6
     7    OrderPriority(String s) {
     8        this.name=s;
     9    }
     10    public String getName() {
     11        return this.name;
     12    }
     13    public String toString() {
     14        return name;
     15    }
    516}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/AnswersRepository.java

    r16f38d4 r0afa35a  
    1111public interface AnswersRepository extends JpaRepository<Answers, AnswerId> {
    1212    List<Answers> findAnswersByAnswerId_Question(Questions q);
     13    int countAnswersByAnswerId_Question(Questions q);
    1314}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/AnswerServiceImpl.java

    r16f38d4 r0afa35a  
    3535            throw new InvalidArgumentsException();
    3636        }
    37         return Optional.of(answersRepository.save(new Answers(question, text)));
     37        return Optional.of(answersRepository.save(new Answers(question, answersRepository.countAnswersByAnswerId_Question(question) +1, text)));
    3838    }
    3939}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/QuestionServiceImpl.java

    r16f38d4 r0afa35a  
    11package com.bazi.fullystocked.Services.Implementations;
    22
     3import com.bazi.fullystocked.Models.Exceptions.ArticleAlreadyInQuestionException;
    34import com.bazi.fullystocked.Models.Exceptions.InvalidArgumentsException;
    45import com.bazi.fullystocked.Models.Managers;
     
    3940            throw new InvalidArgumentsException();
    4041        }
    41         return Optional.of(new Questions(text, worker, manager));
     42        return Optional.of(questionsRepository.save(new Questions(text, worker, manager)));
    4243    }
    4344
     
    4748        Questions question=questionsRepository.findById(questionid).orElseThrow(InvalidArgumentsException::new);
    4849        StoredArticles article=storedArticlesRepository.findById(sarticleid).orElseThrow(InvalidArgumentsException::new);
    49         question.getStoredarticlesList().add(article);
     50        if(article.getQuestionsList().stream().anyMatch(questions -> questions.getQuestionid().equals(questionid)))
     51        {
     52            throw new ArticleAlreadyInQuestionException();
     53        }
     54        article.getQuestionsList().add(question);
     55        storedArticlesRepository.save(article);
    5056        questionsRepository.save(question);
    5157        return Optional.of(question);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Web/Controller/WorkerController.java

    r16f38d4 r0afa35a  
    22
    33import com.bazi.fullystocked.Models.Enumerations.ArticleStatus;
     4import com.bazi.fullystocked.Models.Questions;
    45import com.bazi.fullystocked.Models.SqlViews.ArticlesReport;
    56import com.bazi.fullystocked.Models.Workers;
    6 import com.bazi.fullystocked.Services.ArticlesService;
    7 import com.bazi.fullystocked.Services.OrderedArticlesService;
    8 import com.bazi.fullystocked.Services.StoredArticlesService;
     7import com.bazi.fullystocked.Services.*;
    98import org.springframework.stereotype.Controller;
    109import org.springframework.ui.Model;
     
    2019   private final ArticlesService articlesService;
    2120   private final OrderedArticlesService orderedArticlesService;
     21   private final QuestionsService questionsService;
     22   private final ManagersService managersService;
    2223
    23     public WorkerController(StoredArticlesService storedArticlesService, ArticlesService articlesService, OrderedArticlesService orderedArticlesService) {
     24    public WorkerController(StoredArticlesService storedArticlesService, ArticlesService articlesService, OrderedArticlesService orderedArticlesService, QuestionsService questionsService, ManagersService managersService) {
    2425        this.storedArticlesService = storedArticlesService;
    2526        this.articlesService = articlesService;
    2627        this.orderedArticlesService = orderedArticlesService;
     28        this.questionsService = questionsService;
     29        this.managersService = managersService;
    2730    }
    2831
     
    8487    }
    8588
     89    @PostMapping("/articles/askAQuestion")
     90    public String askAQuestionForASingleArticle(@RequestParam Integer articleId, Model model)
     91    {
     92        model.addAttribute("articleId", articleId);
     93        model.addAttribute("managers", managersService.findAll());
     94        return "AskAQuestionSingle";
     95    }
     96    @PostMapping("/articles/askAQuestion/send")
     97    public String sendQuestionSingle(@RequestParam Integer articleId, @RequestParam Integer managerId, @RequestParam String text, HttpServletRequest request)
     98    {
     99        try
     100        {
     101            Workers u= (Workers) request.getSession().getAttribute("user");
     102            if(storedArticlesService.findById(articleId).isEmpty())
     103            {
     104                return "redirect:/worker/articles";
     105            }
     106            if(!storedArticlesService.findById(articleId).get().getLocationid().equals(u.getLocation().getLocationid()))
     107            {
     108                return "redirect:/login";
     109            }
     110            Questions q=questionsService.create(text, u.getUserid(), managerId).orElseThrow();
     111            questionsService.addArticle(q.getQuestionid(), articleId);
     112            return "redirect:/worker";
     113        }
     114        catch (Exception e)
     115        {
     116            return "redirect:/worker?error="+e.getMessage();
     117        }
     118    }
     119
    86120}
  • FullyStocked/src/main/resources/templates/detailsArticle.html

    r16f38d4 r0afa35a  
    6868      <div class="project-info-box mt-0 mb-0">
    6969        <p class="mb-0">
     70        <form action="/worker/articles/askAQuestion" th:method="POST">
     71          <input type="hidden" name="articleId" th:value="${article.getSarticleid()}">
     72          <button class="btn btn-primary" >Креаирај прашање</button>
     73        </form>
     74
     75        </p>
     76      </div>
     77
     78      <div class="project-info-box mt-3 mb-0">
     79        <p class="mb-0">
    7080          <a class="btn btn-danger" href="/worker/articles" role="button">&laquo; Назад</a>
    7181
Note: See TracChangeset for help on using the changeset viewer.