package me.academeg.controllers;

import me.academeg.entity.Account;
import me.academeg.entity.Article;
import me.academeg.service.AccountService;
import me.academeg.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.UUID;

/**
 * ArticleController Controller
 *
 * @author Yuriy A. Samsonov <yuriy.samsonov96@gmail.com>
 * @version 1.0
 */
@RestController
@RequestMapping("/api/article")
@Validated
public class ArticleController {

    private ArticleService articleService;
    private AccountService accountService;

    @Autowired
    public ArticleController(ArticleService articleService, AccountService accountService) {
        this.articleService = articleService;
        this.accountService = accountService;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Page<Article> get(@RequestParam(defaultValue = "0") int page,
                             @RequestParam(defaultValue = "20") int size) {
        PageRequest pageRequest = new PageRequest(page, size, Sort.Direction.DESC, "timestamp");
        return articleService.getAll(pageRequest);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public Article create(@RequestBody Article article, @AuthenticationPrincipal User user) {
        if (article.getText() == null || article.getText().isEmpty()) {
            throw new IllegalArgumentException("Article cannot be empty");
        }

        Account account = accountService.getByEmail(user.getUsername());
        article.setAuthor(account);
        article.setTimestamp(new Timestamp(System.currentTimeMillis()));
        return articleService.add(article);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public Article edit(@AuthenticationPrincipal User user, @RequestBody Article article) {
        
        return null;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Article delete(@AuthenticationPrincipal User user, @PathVariable UUID id) {

        return null;
    }
}

@@ -2121,32 +2121,37 @@
 apping(value = %22
+/%7Bid%7D
 %22, method = Requ
@@ -2224,16 +2224,39 @@
 er user,
+ @PathVariable UUID id,
  @Reques
@@ -2288,36 +2288,561 @@
         
-%0A        return null
+Article articleFromDb = articleService.getByUuid(id);%0A        if (articleFromDb == null) %7B%0A            throw new IllegalArgumentException(%22WRONG UUID%22);%0A        %7D%0A%0A        Account author = articleFromDb.getAuthor();%0A        Account authAccount = accountService.getByEmail(user.getUsername());%0A        if (!authAccount.getId().equals(author.getId())) %7B%0A            throw new IllegalArgumentException(%22You cannot to edit this article%22);%0A        %7D%0A%0A        articleFromDb.setText(article.getText());%0A        return articleService.edit(articleFromDb)
 ;%0A    %7D%0A
@@ -2921,23 +2921,22 @@
  public 
-Article
+String
  delete(
@@ -3000,28 +3000,718 @@
 ) %7B%0A
-%0A        return null
+        Article articleFromDb = articleService.getByUuid(id);%0A        if (articleFromDb == null) %7B%0A            throw new IllegalArgumentException(%22Wrong UUID%22);%0A        %7D%0A%0A        Account authAccount = accountService.getByEmail(user.getUsername());%0A        if (!authAccount.getAuthority().equals(%22ROLE_MODERATOR%22)%0A                && !authAccount.getAuthority().equals(%22ROLE_ADMIN%22)%0A                && !authAccount.getId().equals(articleFromDb.getAuthor().getId())) %7B%0A            System.out.println(authAccount.getId().equals(articleFromDb.getAuthor().getId()));%0A            throw new IllegalArgumentException(%22You cannot to edit this article%22);%0A        %7D%0A%0A        articleService.delete(id);%0A        return %22ok%22
 ;%0A  

Add opportunite to edit and delete article