Merge remote-tracking branch 'origin/main'
# Conflicts: # src/main/java/site/easystartup/web/project/controller/ProjectController.java
This commit is contained in:
commit
c7cca9a77e
29 files changed
+640
-71
No files matched your search
@@ -1,17 +1,10 @@
|
||||
package site.easystartup.web;
|
||||
|
||||
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
||||
import io.swagger.v3.oas.annotations.info.Info;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@SecurityScheme(type = SecuritySchemeType.OAUTH2, name = "api_key", in = SecuritySchemeIn.HEADER)
|
||||
public class EasyStartupApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package site.easystartup.web.auth.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -18,7 +16,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.request.LoginRequest;
|
||||
import site.easystartup.web.domain.request.SignupRequest;
|
||||
import site.easystartup.web.domain.request.UpdatePassportRequest;
|
||||
import site.easystartup.web.domain.response.MessageResponse;
|
||||
import site.easystartup.web.domain.response.SuccessAuthenticationResponse;
|
||||
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
||||
@@ -29,12 +26,10 @@ import site.easystartup.web.service.AuthService;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.security.Principal;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "Авторизация")
|
||||
public class AuthController {
|
||||
private final JWTUtil jwtUtil;
|
||||
private final UserService userService;
|
||||
@@ -44,7 +39,6 @@ public class AuthController {
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
@PostMapping("/signup")
|
||||
@Operation(summary = "Создание нового аккаунта")
|
||||
public ResponseEntity<Object> registration(@Valid @RequestBody SignupRequest signupRequest,
|
||||
BindingResult bindingResult) {
|
||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
@@ -56,7 +50,6 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
@Operation(summary = "Логин, получение JWT ключа и данных текущего пользователя")
|
||||
public ResponseEntity<Object> authentication(@Valid @RequestBody LoginRequest loginRequest,
|
||||
BindingResult bindingResult) {
|
||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
@@ -75,28 +68,5 @@ public class AuthController {
|
||||
return ResponseEntity.ok().body(response);
|
||||
}
|
||||
|
||||
@PostMapping("/password")
|
||||
@Operation(summary = "обновление пароля и получение нового JWT")
|
||||
public ResponseEntity<Object> updatePassword(@Valid @RequestBody UpdatePassportRequest passportRequest,
|
||||
BindingResult bindingResult,
|
||||
Principal principal) {
|
||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
User user = authService.updatePassword(passportRequest, principal);
|
||||
|
||||
|
||||
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
||||
user.getUsername(),
|
||||
user.getPassword()
|
||||
));
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
String jwt = SecurityConstants.TOKEN_PREFIX + jwtUtil.generateToken(user.getUsername());
|
||||
SuccessAuthenticationResponse response = new SuccessAuthenticationResponse(jwt, modelMapper.map(user, UserDto.class));
|
||||
return ResponseEntity.ok().body(response);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package site.easystartup.web.domain.request;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import site.easystartup.web.domain.annotation.PasswordMatches;
|
||||
import site.easystartup.web.domain.model.Role;
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package site.easystartup.web.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public class NoPermissionException extends RuntimeException {
|
||||
public NoPermissionException(String username) {
|
||||
super("User " + username + " do not have access for this action");
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package site.easystartup.web.notification.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import site.easystartup.web.notification.domian.Notification;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class NotificationController {
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
Logger logger = Logger.getGlobal();
|
||||
|
||||
@GetMapping("/notify")
|
||||
public void notification(Principal principal, @Payload Notification notification) {
|
||||
logger.info("Web socket sent notification to " + principal.getName());
|
||||
messagingTemplate.convertAndSendToUser(principal.getName(), "queue/notification", notification);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package site.easystartup.web.notification.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import site.easystartup.web.notification.domian.Notification;
|
||||
import site.easystartup.web.notification.repo.NotificationRepo;
|
||||
import site.easystartup.web.notification.service.NotificationService;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class NotificationRestController {
|
||||
private final NotificationService notificationService;
|
||||
|
||||
@GetMapping("/notifications/{username}")
|
||||
public ResponseEntity<List<Notification>> notification(@PathVariable String username, Principal principal) {
|
||||
return ResponseEntity.ok().body(notificationService.getNotificationByRecipient(username, principal));
|
||||
}
|
||||
|
||||
@DeleteMapping("/notifications/{id}/delete")
|
||||
public void delete(@PathVariable long id,
|
||||
Principal principal) {
|
||||
notificationService.delete(id, principal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package site.easystartup.web.notification.domian;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
public class Notification {
|
||||
@Id
|
||||
private long id;
|
||||
private String text;
|
||||
private String recipient;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package site.easystartup.web.notification.event;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface EventHandler {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package site.easystartup.web.notification.event;
|
||||
|
||||
public interface NotificationEvent {
|
||||
void invoke();
|
||||
|
||||
String getRecipient();
|
||||
|
||||
String getMessage();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package site.easystartup.web.notification.event;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Scope("singleton")
|
||||
public class NotificationEventManager {
|
||||
private List<NotificationListener> listeners = new ArrayList<>();
|
||||
|
||||
public void registerListener(NotificationListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeListener(NotificationListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public void notifyListeners(Class<? extends NotificationEvent> event) {
|
||||
listeners.forEach(listener -> Arrays.stream(listener.getClass().getMethods()).
|
||||
filter(m -> m.isAnnotationPresent(EventHandler.class)).forEach(method -> {
|
||||
if (method.getParameterTypes()[0].equals(event.getClass())) {
|
||||
try {
|
||||
method.invoke(listener, event);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package site.easystartup.web.notification.event;
|
||||
|
||||
public interface NotificationListener {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package site.easystartup.web.notification.event.realtime;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import site.easystartup.web.notification.domian.Notification;
|
||||
import site.easystartup.web.notification.event.EventHandler;
|
||||
import site.easystartup.web.notification.event.NotificationEvent;
|
||||
import site.easystartup.web.notification.event.NotificationListener;
|
||||
import site.easystartup.web.notification.repo.NotificationRepo;
|
||||
import site.easystartup.web.notification.service.NotificationService;
|
||||
import site.easystartup.web.post.domain.event.RepostEvent;
|
||||
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class RealTimeListener implements NotificationListener {
|
||||
private final NotificationService notificationService;
|
||||
|
||||
private final NotificationRepo repo;
|
||||
|
||||
@EventHandler
|
||||
public void onEvent(NotificationEvent event) {
|
||||
Notification notification = new Notification();
|
||||
notification.setText(event.getMessage());
|
||||
notification.setRecipient(event.getRecipient());
|
||||
notificationService.sendNotification(repo.save(notification));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.notification.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import site.easystartup.web.notification.domian.Notification;
|
||||
|
||||
public interface NotificationRepo extends JpaRepository<Notification, Long> {
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package site.easystartup.web.notification.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import site.easystartup.web.exception.NoPermissionException;
|
||||
import site.easystartup.web.notification.domian.Notification;
|
||||
import site.easystartup.web.notification.repo.NotificationRepo;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class NotificationService {
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
private final NotificationRepo repo;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public void sendNotification(Notification notification) {
|
||||
messagingTemplate.convertAndSendToUser(notification.getRecipient(), "queue/notification", notification);
|
||||
}
|
||||
|
||||
public List<Notification> getNotificationByRecipient(String recipient, Principal principal) {
|
||||
if(!recipient.equals(principal.getName()) && !userService.getUserByPrincipal(principal).isAdmin())
|
||||
throw new NoPermissionException(String.format("User %s dont have access to see notifications of user %s",
|
||||
principal.getName(), recipient));
|
||||
return repo.findAll().stream()
|
||||
.filter(notification -> notification.getRecipient().equals(recipient)).toList();
|
||||
}
|
||||
|
||||
public void delete(long id, Principal principal) {
|
||||
Notification notification = repo.findById(id).orElseThrow();
|
||||
if(!notification.getRecipient().equals(principal.getName()) && !userService.getUserByPrincipal(principal).isAdmin())
|
||||
throw new NoPermissionException(String.format("User %s dont have access to delete notifications of user %s",
|
||||
principal.getName(), notification.getRecipient()));
|
||||
repo.delete(notification);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package site.easystartup.web.post.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.response.MessageResponse;
|
||||
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
||||
import site.easystartup.web.exception.NoPermissionException;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
import site.easystartup.web.post.domain.request.PostRequest;
|
||||
import site.easystartup.web.post.dto.PostDto;
|
||||
import site.easystartup.web.post.service.PostService;
|
||||
import site.easystartup.web.project.domain.requst.ProjectRequest;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class PostController {
|
||||
private final PostService postService;
|
||||
private final UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final ResponseErrorValidation responseErrorValidation;
|
||||
|
||||
@GetMapping("/")
|
||||
public ResponseEntity<List<PostDto>> main(Principal principal) {
|
||||
var posts = postService.getAllPostsByAuthor(principal.getName())
|
||||
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
||||
return ResponseEntity.ok().body(posts);
|
||||
}
|
||||
|
||||
@PostMapping("/new")
|
||||
public ResponseEntity<Object> createPost(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
||||
Principal principal) {
|
||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
Post post = postService.createPost(postRequest, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
||||
}
|
||||
|
||||
@GetMapping("/user/{username}")
|
||||
public ResponseEntity<List<PostDto>> user(@PathVariable String username) {
|
||||
var posts = postService.getAllPostsByAuthor(username)
|
||||
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
||||
return ResponseEntity.ok().body(posts);
|
||||
}
|
||||
|
||||
@GetMapping("/{post_id}/edit/")
|
||||
public ResponseEntity<Object> edit(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
||||
Principal principal, @PathVariable long post_id) {
|
||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
Post post = postService.edit(postRequest, post_id, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{post_id}/delete")
|
||||
public void delete(@PathVariable long post_id,
|
||||
Principal principal) {
|
||||
Post post = postService.getPostById(post_id);
|
||||
if(!post.getSender().equals(principal.getName()) && !userService.getUserByPrincipal(principal).isAdmin())
|
||||
throw new NoPermissionException(String.format("User %s cant delete post %s", principal.getName(), post_id));
|
||||
else postService.delete(post);
|
||||
}
|
||||
|
||||
@PutMapping("/{post_id}/like/")
|
||||
public ResponseEntity<Object> like(Principal principal, @PathVariable long post_id) {
|
||||
Post post = postService.getPostById(post_id);
|
||||
|
||||
Post edited = postService.addLike(post, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(edited, PostRequest.class));
|
||||
}
|
||||
|
||||
@PutMapping("/{post_id}/dislike/")
|
||||
public ResponseEntity<Object> dislike(Principal principal, @PathVariable long post_id) {
|
||||
Post post = postService.getPostById(post_id);
|
||||
|
||||
Post edited = postService.disLike(post, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(edited, PostRequest.class));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package site.easystartup.web.post.domain.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import site.easystartup.web.notification.event.NotificationEvent;
|
||||
import site.easystartup.web.notification.event.NotificationEventManager;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class RepostEvent implements NotificationEvent {
|
||||
@Autowired
|
||||
private NotificationEventManager notificationEventManager;
|
||||
|
||||
@Getter
|
||||
private final String recipient;
|
||||
|
||||
@Getter
|
||||
private final String reportedUser;
|
||||
|
||||
@Getter
|
||||
private final String message;
|
||||
|
||||
@Override
|
||||
public void invoke() {
|
||||
notificationEventManager.notifyListeners(getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.post.domain.exception;
|
||||
|
||||
public class AlreadyLikedException extends RuntimeException {
|
||||
public AlreadyLikedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.post.domain.exception;
|
||||
|
||||
public class DontLickedException extends RuntimeException{
|
||||
public DontLickedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package site.easystartup.web.post.domain.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public class PostNotFoundException extends RuntimeException implements Supplier<PostNotFoundException> {
|
||||
private String message;
|
||||
|
||||
public PostNotFoundException(String message) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostNotFoundException get() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package site.easystartup.web.post.domain.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.Hibernate;
|
||||
import site.easystartup.web.domain.model.Role;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@RequiredArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "pst")
|
||||
public class Post implements Comparable<Post>{
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "post_id")
|
||||
private long id;
|
||||
@Column(columnDefinition="varchar(1024)")
|
||||
private String text;
|
||||
private String sender;
|
||||
@Column(name = "lk")
|
||||
private long like;
|
||||
|
||||
@ElementCollection(targetClass = Long.class, fetch = FetchType.LAZY)
|
||||
@CollectionTable(name = "post_answers", joinColumns = @JoinColumn(name = "post_id"))
|
||||
private Set<Long> answers;
|
||||
|
||||
@ElementCollection(targetClass = String.class, fetch = FetchType.LAZY)
|
||||
@CollectionTable(name = "post_liked", joinColumns = @JoinColumn(name = "post_id"))
|
||||
private Set<String> liked_users;
|
||||
|
||||
private Long answered_post;
|
||||
|
||||
@Column(name = "date", updatable = false)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date date;
|
||||
|
||||
private boolean edited;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
|
||||
Post that = (Post) o;
|
||||
return Objects.equals(id, that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getClass().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Post o) {
|
||||
if (getDate() == null || o.getDate() == null)
|
||||
return 0;
|
||||
return getDate().compareTo(o.getDate());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package site.easystartup.web.post.domain.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class PostRequest {
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
private String sender;
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private List<String> liked_users;
|
||||
private long like;
|
||||
private List<Long> answers;
|
||||
private Long answered_post;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package site.easystartup.web.post.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostDto implements Serializable {
|
||||
private long id;
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
private String sender;
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private List<String> liked_users;
|
||||
private long like;
|
||||
private List<Long> answers;
|
||||
private long answered_post;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.post.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
|
||||
public interface PostRepo extends JpaRepository<Post, Long> {
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package site.easystartup.web.post.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import site.easystartup.web.post.domain.event.RepostEvent;
|
||||
import site.easystartup.web.post.domain.exception.AlreadyLikedException;
|
||||
import site.easystartup.web.post.domain.exception.PostNotFoundException;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
import site.easystartup.web.post.domain.request.PostRequest;
|
||||
import site.easystartup.web.post.repo.PostRepo;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class PostService {
|
||||
private final PostRepo postRepo;
|
||||
|
||||
public Post getPostById(long id) {
|
||||
return postRepo.findById(id).orElseThrow(new PostNotFoundException("Post with id " + id + " dont exists!"));
|
||||
}
|
||||
|
||||
public List<Post> getAllPostsByAuthor(String username) {
|
||||
return postRepo.findAll().stream().filter(post -> post.getSender().equals(username)).toList();
|
||||
}
|
||||
|
||||
public Post createPost(PostRequest postRequest, Principal principal) {
|
||||
Post post = new Post();
|
||||
post.setAnswered_post(post.getAnswered_post());
|
||||
post.setAnswers(post.getAnswers());
|
||||
post.setDate(new Date());
|
||||
post.setEdited(false);
|
||||
post.setLike(0);
|
||||
post.setSender(principal.getName());
|
||||
post.setText(postRequest.getText());
|
||||
checkAnswers(post);
|
||||
return postRepo.save(post);
|
||||
}
|
||||
|
||||
public Post edit(PostRequest postRequest, long post_id, Principal principal) {
|
||||
Post post = postRepo.findById(post_id).orElseThrow(
|
||||
new PostNotFoundException("Post with id " + post_id + " didnt exists"));
|
||||
post.setEdited(true);
|
||||
post.setSender(principal.getName());
|
||||
post.setText(postRequest.getText());
|
||||
checkAnswers(post);
|
||||
return postRepo.save(post);
|
||||
}
|
||||
|
||||
public void delete(Post post) {
|
||||
postRepo.delete(post);
|
||||
}
|
||||
|
||||
public void checkAnswers(Post post) {
|
||||
if(post.getAnswered_post() != null) {
|
||||
Post answered = postRepo.findById(post.getAnswered_post()).orElseThrow(
|
||||
new PostNotFoundException("Post didnt exists: " + post.getAnswered_post()));
|
||||
Set<Long> answers = answered.getAnswers() != null ? answered.getAnswers() : new TreeSet<>();
|
||||
answers.add(post.getId());
|
||||
answered.setAnswers(answers);
|
||||
new RepostEvent(answered.getSender(),
|
||||
post.getSender(),
|
||||
String.format("Пользователь %s ответил на вашу запись!", answered.getSender())).invoke();
|
||||
postRepo.save(answered);
|
||||
}
|
||||
}
|
||||
|
||||
public Post addLike(Post post, Principal principal) {
|
||||
if (post.getLiked_users().contains(principal.getName()))
|
||||
throw new AlreadyLikedException(String.format("User %s already liked post %s", principal.getName(), post.getId()));
|
||||
post.setLike(post.getLike() + 1);
|
||||
Set<String> liked = post.getLiked_users() == null ? new TreeSet<>() : post.getLiked_users();
|
||||
liked.add(principal.getName());
|
||||
return postRepo.save(post);
|
||||
}
|
||||
|
||||
public Post disLike(Post post, Principal principal) {
|
||||
if (!post.getLiked_users().contains(principal.getName()))
|
||||
throw new AlreadyLikedException(String.format("User %s already liked post %s", principal.getName(), post.getId()));
|
||||
post.setLike(post.getLike() - 1);
|
||||
Set<String> liked = post.getLiked_users() == null ? new TreeSet<>() : post.getLiked_users();
|
||||
liked.remove(principal.getName());
|
||||
return postRepo.save(post);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/project")
|
||||
@RequestMapping("/api/project")
|
||||
@Tag(name = "Проекты", description = "контроллер для работы с проектами")
|
||||
public class ProjectController {
|
||||
private final ProjectService projectService;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package site.easystartup.web.project.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
@@ -33,29 +33,18 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
// http
|
||||
// .authorizeRequests()
|
||||
// .antMatchers("/swagger-ui.html").permitAll()
|
||||
// .antMatchers("/swagger-ui/index.html").permitAll()
|
||||
// .antMatchers("/webjars/**").permitAll()
|
||||
// .antMatchers("/swagger-resources/**").permitAll()
|
||||
// .antMatchers("/v3/*").permitAll()
|
||||
// .antMatchers("/csrf").permitAll()
|
||||
// .antMatchers("/").permitAll()
|
||||
// .and()
|
||||
http.cors().and().csrf().disable()
|
||||
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
|
||||
.and()
|
||||
.sessionManagement()
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers(SecurityConstants.SIGN_UP_URLS).permitAll()
|
||||
.antMatchers("/").permitAll()
|
||||
.anyRequest().authenticated();
|
||||
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
|
||||
|
||||
// .cors().and().csrf().disable()
|
||||
// .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
|
||||
// .and()
|
||||
// .sessionManagement()
|
||||
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
// .and()
|
||||
// .authorizeRequests()
|
||||
// .antMatchers(SecurityConstants.SIGN_UP_URLS).permitAll()
|
||||
// .anyRequest().authenticated();
|
||||
//
|
||||
// http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -7,10 +7,8 @@ import site.easystartup.web.domain.exception.UserExistException;
|
||||
import site.easystartup.web.domain.model.Role;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.request.SignupRequest;
|
||||
import site.easystartup.web.domain.request.UpdatePassportRequest;
|
||||
import site.easystartup.web.repo.UserRepo;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -18,7 +16,6 @@ import java.util.Set;
|
||||
@RequiredArgsConstructor
|
||||
public class AuthService {
|
||||
private final UserRepo userRepo;
|
||||
private final UserService userService;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public User createUser(SignupRequest signupRequest) {
|
||||
@@ -36,11 +33,4 @@ public class AuthService {
|
||||
|
||||
return userRepo.save(user);
|
||||
}
|
||||
|
||||
public User updatePassword(UpdatePassportRequest passportRequest, Principal principal) {
|
||||
User user = userService.getUserByPrincipal(principal);
|
||||
user.setPassword(passwordEncoder.encode(passportRequest.getPassword()));
|
||||
|
||||
return userRepo.save(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
function connect() {
|
||||
let socket = new SockJs(`api/socket`);
|
||||
|
||||
let stompClient = Stomp.over(socket);
|
||||
|
||||
return stompClient;
|
||||
}
|
||||
|
||||
|
||||
let stompClient = this.webSocketService.connect();
|
||||
|
||||
stompClient.connect({}, frame => {
|
||||
|
||||
stompClient.subscribe('/user/queue/notification', notification => {
|
||||
console.log('test'+ notification)
|
||||
JSON.parse(notification.body);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
const sendMessage = (msg) => {
|
||||
if (msg.trim() !== "") {
|
||||
const message = {
|
||||
senderId: currentUser.id,
|
||||
recipientId: activeContact.id,
|
||||
senderName: currentUser.name,
|
||||
recipientName: activeContact.name,
|
||||
content: msg,
|
||||
};
|
||||
|
||||
stompClient.send("/app/chat", {}, JSON.stringify(message));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user