start backend
This commit is contained in:
@@ -1,4 +1,27 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.AuthRequest;
|
||||
import com.krylov.refound.dto.AuthResponse;
|
||||
import com.krylov.refound.service.security.AuthService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequiredArgsConstructor
|
||||
public class AuthController {
|
||||
private final AuthService service;
|
||||
|
||||
@PostMapping("/register")
|
||||
public AuthResponse register(@RequestBody AuthRequest request) {
|
||||
return service.register(request);
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public AuthResponse login(@RequestBody AuthRequest request) {
|
||||
return service.login(request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.ChatMessage;
|
||||
import com.krylov.refound.entity.Message;
|
||||
import com.krylov.refound.entity.User;
|
||||
import com.krylov.refound.repository.UserRepository;
|
||||
import com.krylov.refound.service.MessageService;
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class ChatController {
|
||||
private final MessageService service;
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
|
||||
@MessageMapping("/chat")
|
||||
public void send(@Payload ChatMessage message, Principal principal) {
|
||||
|
||||
Optional<User> sender = userRepository.findByEmail(principal.getName());
|
||||
Message saved = new Message();
|
||||
if(sender.isPresent()){
|
||||
saved = service.send(message.getReceiverId(), message.getContent());
|
||||
}
|
||||
|
||||
// отправка получателю
|
||||
messagingTemplate.convertAndSend("/topic/messages/" + message.getReceiverId(), saved);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.PostResponse;
|
||||
import com.krylov.refound.mapper.PostMapper;
|
||||
import com.krylov.refound.service.FavoriteService;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/favorites")
|
||||
@RequiredArgsConstructor
|
||||
public class FavoriteController {
|
||||
|
||||
private final FavoriteService service;
|
||||
private final PostMapper mapper;
|
||||
|
||||
@PostMapping("/{postId}/toggle")
|
||||
public void toggle(@PathVariable Long postId) {
|
||||
service.toggle(postId);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<PostResponse> getAll() {
|
||||
return service.getFavorites()
|
||||
.stream()
|
||||
.map(mapper::toResponse)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.service.FileStorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/files")
|
||||
@RequiredArgsConstructor
|
||||
public class FileController {
|
||||
private final FileStorageService storageService;
|
||||
|
||||
@PostMapping
|
||||
public String upload(@RequestParam("file") MultipartFile file) {
|
||||
return storageService.save(file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.DialogResponse;
|
||||
import com.krylov.refound.entity.Message;
|
||||
import com.krylov.refound.service.MessageService;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/messages")
|
||||
@RequiredArgsConstructor
|
||||
public class MessageController {
|
||||
private final MessageService service;
|
||||
|
||||
@PostMapping
|
||||
public void send(@RequestParam Long receiverId,
|
||||
@RequestParam String content) {
|
||||
service.send(receiverId, content);
|
||||
}
|
||||
|
||||
@GetMapping("/{userId}")
|
||||
public List<Message> chat(@PathVariable Long userId) {
|
||||
return service.getChat(userId);
|
||||
}
|
||||
|
||||
@GetMapping("/dialogs")
|
||||
public List<DialogResponse> getDialogs() {
|
||||
return service.getDialogs();
|
||||
}
|
||||
|
||||
@PostMapping("/read/{userId}")
|
||||
public void markAsRead(@PathVariable Long userId) {
|
||||
service.markAsRead(userId);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,50 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.PostFilter;
|
||||
import com.krylov.refound.dto.PostRequest;
|
||||
import com.krylov.refound.dto.PostResponse;
|
||||
import com.krylov.refound.repository.PostRepository;
|
||||
import com.krylov.refound.service.PostService;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.MediaType;
|
||||
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.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/posts")
|
||||
@RequiredArgsConstructor
|
||||
public class PostController {
|
||||
|
||||
private final PostService service;
|
||||
|
||||
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public PostResponse create(@RequestPart("data") PostRequest request, @RequestPart(value = "files", required = false) List<MultipartFile> files) {
|
||||
return service.create(request, files);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Page<PostResponse> getFeed(PostFilter filter, Pageable pageable) {
|
||||
return service.getFeed(filter, pageable);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public PostResponse update(@PathVariable Long id, @RequestBody PostRequest request) {
|
||||
return service.update(id, request);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable Long id) {
|
||||
service.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user