Ignore:
Timestamp:
02/04/22 23:22:13 (3 years ago)
Author:
Test <matonikolov77@…>
Branches:
main
Children:
5b447b0
Parents:
3ded84d
Message:

Added CRUD for movies,persons,discussion,replies,genres
Added ajaxcalls

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.web.controller;
    22
    3 import com.wediscussmovies.project.model.*;
     3import com.wediscussmovies.project.LoggedUser;
     4import com.wediscussmovies.project.model.Reply;
     5import com.wediscussmovies.project.model.User;
     6import com.wediscussmovies.project.model.primarykeys.ReplyPK;
    47import com.wediscussmovies.project.service.ReplyService;
     8import org.springframework.security.authentication.AnonymousAuthenticationToken;
     9import org.springframework.security.core.Authentication;
     10import org.springframework.security.core.context.SecurityContextHolder;
     11import org.springframework.security.core.userdetails.UserDetails;
    512import org.springframework.stereotype.Controller;
    613import org.springframework.ui.Model;
    7 import org.springframework.web.bind.annotation.GetMapping;
    8 import org.springframework.web.bind.annotation.PathVariable;
    9 import org.springframework.web.bind.annotation.PostMapping;
    10 import org.springframework.web.bind.annotation.RequestParam;
     14import org.springframework.web.bind.annotation.*;
    1115
    12 import java.util.Optional;
    1316
    1417@Controller
     18@RequestMapping("/replies")
    1519public class ReplyController {
    1620    private final ReplyService replyService;
     
    2024    }
    2125
    22     @GetMapping("/edit/{id}")
    23     public String getReplyEdit(@PathVariable Long id, Model model){
    24         Optional<Reply> reply = replyService.findById(id);
    25         if(reply.isEmpty())
    26             return "redirect:/movies";
    27         model.addAttribute("reply", reply.get());
    28         model.addAttribute("templateContext", "replyEdit");
     26    @GetMapping("/add/{discussionId}")
     27    public String addForm(@PathVariable Integer discussionId, Model model){
     28        model.addAttribute("discussionId",discussionId);
     29        model.addAttribute("contentTemplate","repliesAdd");
    2930        return "template";
    3031    }
    3132
    32     @PostMapping("/edit/confirm/{id}")
    33     public String getReplyEdit(@PathVariable Long id, @RequestParam String text){
    34         Optional<Reply> replyOp = replyService.findById(id);
    35         if(replyOp.isEmpty())
    36             return "redirect:/discussions";
    37         Reply reply = replyOp.get();
    38         replyService.delete(reply);
    39         reply.setText(text);
    40         replyService.save(reply);
    41         return "redirect:/discussions/"+reply.getDiscussionId();
     33    @GetMapping("/edit/{discussionId}/{replyId}")
     34    public String editForm(@PathVariable Integer discussionId,@PathVariable Integer replyId, Model model){
     35
     36        try {
     37            Reply reply = replyService.findById(discussionId,replyId);
     38            model.addAttribute("reply", reply);
     39            model.addAttribute("contentTemplate", "repliesAdd");
     40            return "template";
     41
     42
     43        }catch (RuntimeException exc){
     44            return "redirect:/discussions?error="+exc.getMessage();
     45
     46        }
    4247    }
     48    @PostMapping("/save")
     49    public String saveReply(
     50            @RequestParam Integer discussionId,
     51            @RequestParam String text){
     52
     53        try {
     54
     55            this.replyService.save(discussionId,text, LoggedUser.getLoggedUser());
     56            return "redirect:/discussions/"+discussionId;
     57
     58        }
     59        catch (RuntimeException exc){
     60            return "redirect:/discussions?error="+exc.getMessage();
     61
     62        }
     63    }
     64
     65    @PostMapping("/save/{replyId}")
     66    public String editReply(
     67                            @PathVariable Integer replyId,
     68                            @RequestParam Integer discussionId,
     69                            @RequestParam String text){
     70
     71        /*
     72            1. Da se realizira so ajax baranje na restController
     73         */
     74        try {
     75            Reply reply = replyService.edit(replyId,discussionId, text);
     76
     77            return "redirect:/discussions/" + reply.getDiscussion().getDiscussionId();
     78        }
     79        catch (RuntimeException exc){
     80            return "redirect:/discussions?error="+exc.getMessage();
     81        }
     82
     83    }
     84
    4385}
Note: See TracChangeset for help on using the changeset viewer.