Skip to content

Commit

Permalink
feat: ajax rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
gutche committed Mar 21, 2023
1 parent f9587c2 commit a020e36
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import net.daw.alist.models.Post;
import net.daw.alist.models.User;
import net.daw.alist.services.PostService;
import net.daw.alist.services.UserService;
import net.daw.alist.services.VotesService;
import net.daw.alist.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -29,7 +27,7 @@ public String home(Model model) {
}

@GetMapping("/posts")
public String getNewPosts(Model model, @RequestParam int page, Authentication authentication) {
public String getNewPosts(Model model, @RequestParam int page) {
Page<Post> newPage = postService.getPosts(page);
model.addAttribute("posts", newPage);
return "post";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
package net.daw.alist.controllers.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import net.daw.alist.models.Post;
import net.daw.alist.models.User;
import net.daw.alist.services.PostService;

@RestController
@RequestMapping("/api/ajax")
public class AjaxRestController {

@Autowired
PostService postService;

@GetMapping("/posts")
public Page<Post> getNewPosts(@RequestParam int page) {
if (page <= (int) Math.ceil(postService.count()/2))
return postService.getPosts(page);
return null;
}

@GetMapping("/followed-users/posts")
public Page<Post> getFollowedUsersPosts(Authentication authentication, @RequestParam int page) {
User currentUser = (User) authentication.getPrincipal();

if (page <= (int) Math.ceil(postService.count()/2))
return postService.getStarredPosts(page, currentUser.getId().intValue());

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

import javax.transaction.Transactional;

@Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected void configure(HttpSecurity http) throws Exception {

// URLs that need authentication to access to it
http
.authorizeRequests().antMatchers(HttpMethod.POST, "/api/ajax/**").hasRole("USER")
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/api/ajax/followed-users/posts").hasAnyRole("ADMIN", "USER")
.antMatchers(HttpMethod.POST, "/api/comments/**").hasAnyRole("ADMIN", "USER")
.antMatchers(HttpMethod.DELETE, "/api/comments/**").hasAnyRole("ADMIN", "USER")
.antMatchers(HttpMethod.POST, "/api/posts/**").hasAnyRole("ADMIN", "USER")
Expand Down
4 changes: 4 additions & 0 deletions back/src/main/java/net/daw/alist/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public void delete(Post post) {
postRepository.delete(post);
}

public long count() {
return postRepository.count();
}

}

0 comments on commit a020e36

Please sign in to comment.