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.PageRequest;
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 javax.xml.datatype.DatatypeConstants;
import java.sql.Timestamp;
import java.util.Calendar;
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 Iterable<Article> get(@RequestParam int page, @RequestParam int count) {
        PageRequest pageRequest = new PageRequest(page, count);
        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;
    }
}

@@ -298,16 +298,61 @@
 equest;%0A
+import org.springframework.data.domain.Sort;%0A
 import o
@@ -605,94 +605,22 @@
 java
-x.xml.datatype.DatatypeConstants;%0Aimport java.sql.Timestamp;%0Aimport java.util.Calendar
+.sql.Timestamp
 ;%0Aim
@@ -1255,32 +1255,106 @@
 aram
- int page, @RequestParam
+(defaultValue = %220%22) int page,%0A                                 @RequestParam(defaultValue = %2220%22)
  int
@@ -1420,24 +1420,58 @@
 (page, count
+, Sort.Direction.DESC, %22timestamp%22
 );%0A        r

Add default value for page and count and add sort by timestamp