Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@
</execution>
</executions>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.sopromadze.blogapi.utils.AppConstants;
import com.sopromadze.blogapi.utils.AppUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -56,18 +55,18 @@ public PagedResponse<AlbumResponse> getAllAlbums(

@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Album> addAlbum(@Valid @RequestBody AlbumRequest albumRequest, @CurrentUser UserPrincipal currentUser) {
public Album addAlbum(@Valid @RequestBody AlbumRequest albumRequest, @CurrentUser UserPrincipal currentUser) {
return albumService.addAlbum(albumRequest, currentUser);
}

@GetMapping("/{id}")
public ResponseEntity<Album> getAlbum(@PathVariable(name = "id") Long id) {
public Album getAlbum(@PathVariable(name = "id") Long id) {
return albumService.getAlbum(id);
}

@PutMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<AlbumResponse> updateAlbum(@PathVariable(name = "id") Long id, @Valid @RequestBody AlbumRequest newAlbum,
public AlbumResponse updateAlbum(@PathVariable(name = "id") Long id, @Valid @RequestBody AlbumRequest newAlbum,
@CurrentUser UserPrincipal currentUser) {
return albumService.updateAlbum(id, newAlbum, currentUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ public PagedResponse<Category> getAllCategories(

@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Category> addCategory(@Valid @RequestBody Category category,
public Category addCategory(@Valid @RequestBody Category category,
@CurrentUser UserPrincipal currentUser) {

return categoryService.addCategory(category, currentUser);
}

@GetMapping("/{id}")
public ResponseEntity<Category> getCategory(@PathVariable(name = "id") Long id) {
public Category getCategory(@PathVariable(name = "id") Long id) {
return categoryService.getCategory(id);
}

@PutMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<Category> updateCategory(@PathVariable(name = "id") Long id,
public Category updateCategory(@PathVariable(name = "id") Long id,
@Valid @RequestBody Category category, @CurrentUser UserPrincipal currentUser) throws UnauthorizedException {
return categoryService.updateCategory(id, category, currentUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ public class CommentController {
private final CommentService commentService;

@GetMapping
public ResponseEntity<PagedResponse<Comment>> getAllComments(@PathVariable(name = "postId") Long postId,
public PagedResponse<Comment> getAllComments(@PathVariable(name = "postId") Long postId,
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {

PagedResponse<Comment> allComments = commentService.getAllComments(postId, page, size);

return new ResponseEntity< >(allComments, HttpStatus.OK);
return allComments;
}

@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Comment> addComment(@Valid @RequestBody CommentRequest commentRequest,
public Comment addComment(@Valid @RequestBody CommentRequest commentRequest,
@PathVariable(name = "postId") Long postId, @CurrentUser UserPrincipal currentUser) {
Comment newComment = commentService.addComment(commentRequest, postId, currentUser);

return new ResponseEntity<>(newComment, HttpStatus.CREATED);
return newComment;
}

@GetMapping("/{id}")
public ResponseEntity<Comment> getComment(@PathVariable(name = "postId") Long postId,
public Comment getComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "id") Long id) {
Comment comment = commentService.getComment(postId, id);

return new ResponseEntity<>(comment, HttpStatus.OK);
return comment;
}

@PutMapping("/{id}")
Expand All @@ -74,7 +74,7 @@ public ResponseEntity<Comment> updateComment(@PathVariable(name = "postId") Long
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<ApiResponse> deleteComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {

ApiResponse response = commentService.deleteComment(postId, id, currentUser);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.sopromadze.blogapi.security;

import com.sopromadze.blogapi.service.CustomUserDetailsService;
import com.sopromadze.blogapi.service.impl.CustomUserDetailsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand All @@ -27,6 +30,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtTokenProvider tokenProvider;

/*@Autowired
@Qualifier("customUserDetailsServiceImpl")
private CustomUserDetailsServiceImpl customUserDetailsService;*/
private final CustomUserDetailsService customUserDetailsService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public interface AlbumService {

PagedResponse<AlbumResponse> getAllAlbums(int page, int size);

ResponseEntity<Album> addAlbum(AlbumRequest albumRequest, UserPrincipal currentUser);
Album addAlbum(AlbumRequest albumRequest, UserPrincipal currentUser);

ResponseEntity<Album> getAlbum(Long id);
Album getAlbum(Long id);

ResponseEntity<AlbumResponse> updateAlbum(Long id, AlbumRequest newAlbum, UserPrincipal currentUser);
AlbumResponse updateAlbum(Long id, AlbumRequest newAlbum, UserPrincipal currentUser);

ResponseEntity<ApiResponse> deleteAlbum(Long id, UserPrincipal currentUser);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public interface CategoryService {

PagedResponse<Category> getAllCategories(int page, int size);

ResponseEntity<Category> getCategory(Long id);
Category getCategory(Long id);

ResponseEntity<Category> addCategory(Category category, UserPrincipal currentUser);
Category addCategory(Category category, UserPrincipal currentUser);

ResponseEntity<Category> updateCategory(Long id, Category newCategory, UserPrincipal currentUser)
Category updateCategory(Long id, Category newCategory, UserPrincipal currentUser)
throws UnauthorizedException;

ResponseEntity<ApiResponse> deleteCategory(Long id, UserPrincipal currentUser) throws UnauthorizedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sopromadze.blogapi.payload.CommentRequest;
import com.sopromadze.blogapi.payload.PagedResponse;
import com.sopromadze.blogapi.security.UserPrincipal;
import org.springframework.http.ResponseEntity;

public interface CommentService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public PagedResponse<AlbumResponse> getAllAlbums(int page, int size) {
}

@Override
public ResponseEntity<Album> addAlbum(AlbumRequest albumRequest, UserPrincipal currentUser) {
public Album addAlbum(AlbumRequest albumRequest, UserPrincipal currentUser) {
User user = userRepository.getUser(currentUser);

Album album = new Album();
Expand All @@ -77,17 +77,17 @@ public ResponseEntity<Album> addAlbum(AlbumRequest albumRequest, UserPrincipal c

album.setUser(user);
Album newAlbum = albumRepository.save(album);
return new ResponseEntity<>(newAlbum, HttpStatus.CREATED);
return album;
}

@Override
public ResponseEntity<Album> getAlbum(Long id) {
public Album getAlbum(Long id) {
Album album = albumRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(ALBUM_STR, ID, id));
return new ResponseEntity<>(album, HttpStatus.OK);
return album;
}

@Override
public ResponseEntity<AlbumResponse> updateAlbum(Long id, AlbumRequest newAlbum, UserPrincipal currentUser) {
public AlbumResponse updateAlbum(Long id, AlbumRequest newAlbum, UserPrincipal currentUser) {
Album album = albumRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(ALBUM_STR, ID, id));
User user = userRepository.getUser(currentUser);
if (album.getUser().getId().equals(user.getId()) || currentUser.getAuthorities()
Expand All @@ -99,7 +99,7 @@ public ResponseEntity<AlbumResponse> updateAlbum(Long id, AlbumRequest newAlbum,

modelMapper.map(updatedAlbum, albumResponse);

return new ResponseEntity<>(albumResponse, HttpStatus.OK);
return albumResponse;
}

throw new BlogapiException(HttpStatus.UNAUTHORIZED, YOU_DON_T_HAVE_PERMISSION_TO_MAKE_THIS_OPERATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ public PagedResponse<Category> getAllCategories(int page, int size) {
}

@Override
public ResponseEntity<Category> getCategory(Long id) {
public Category getCategory(Long id) {
Category category = categoryRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Category", "id", id));
return new ResponseEntity<>(category, HttpStatus.OK);
return category;
}

@Override
public ResponseEntity<Category> addCategory(Category category, UserPrincipal currentUser) {
public Category addCategory(Category category, UserPrincipal currentUser) {
Category newCategory = categoryRepository.save(category);
return new ResponseEntity<>(newCategory, HttpStatus.CREATED);
return newCategory;
}

@Override
public ResponseEntity<Category> updateCategory(Long id, Category newCategory, UserPrincipal currentUser) {
public Category updateCategory(Long id, Category newCategory, UserPrincipal currentUser) {
Category category = categoryRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Category", "id", id));
if (category.getCreatedBy().equals(currentUser.getId()) || currentUser.getAuthorities()
.contains(new SimpleGrantedAuthority(RoleName.ROLE_ADMIN.toString()))) {
category.setName(newCategory.getName());
Category updatedCategory = categoryRepository.save(category);
return new ResponseEntity<>(updatedCategory, HttpStatus.OK);
return category;
}

throw new UnauthorizedException("You don't have permission to edit this category");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import javax.transaction.Transactional;

@Service
@Service("customUserDetailsServiceImpl")
@RequiredArgsConstructor
public class CustomUserDetailsServiceImpl implements UserDetailsService, CustomUserDetailsService {

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/_application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ app.jwtExpirationInMs=3600000
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring.jackson.time-zone=UTC
cors.allowedOrings=*

spring.main.allow-bean-definition-overriding=true
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

spring:
profiles:
active: "dev"
active: "dev"


main:
allow-bean-definition-overriding: true
Loading