Merge pull request #3 from SlimusMinus/del_front
del front and add auth
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.dto.AuthRequest;
|
||||
import com.krylov.refound.dto.AuthResponse;
|
||||
import com.krylov.refound.dto.RegisterRequest;
|
||||
import com.krylov.refound.entity.User;
|
||||
import com.krylov.refound.repository.UserRepository;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/auth")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AuthController {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<?> register(@RequestBody RegisterRequest request) {
|
||||
|
||||
// Проверка что логин не занят
|
||||
if (userRepository.findByLogin(request.getLogin()).isPresent()) {
|
||||
return ResponseEntity
|
||||
.badRequest()
|
||||
.body(Map.of("message", "Логин уже занят"));
|
||||
}
|
||||
|
||||
// Создание пользователя
|
||||
User user = new User();
|
||||
user.setName(request.getName());
|
||||
user.setLogin(request.getLogin());
|
||||
user.setPassword(request.getPassword()); // позже добавить BCrypt
|
||||
|
||||
userRepository.save(user);
|
||||
|
||||
String token = "fake-jwt-token";
|
||||
|
||||
return ResponseEntity.ok(new AuthResponse(token, user));
|
||||
}
|
||||
|
||||
@GetMapping("/check-login")
|
||||
public ResponseEntity<?> checkLogin(@RequestParam String login) {
|
||||
boolean exists = userRepository.existsByLogin(login);
|
||||
return ResponseEntity.ok(Map.of("exists", exists));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class ChatController {
|
||||
@MessageMapping("/chat")
|
||||
public void send(@Payload ChatMessage message, Principal principal) {
|
||||
|
||||
Optional<User> sender = userRepository.findByEmail(principal.getName());
|
||||
Optional<User> sender = userRepository.findByLogin(principal.getName());
|
||||
Message saved = new Message();
|
||||
if(sender.isPresent()){
|
||||
saved = service.send(message.getReceiverId(), message.getContent());
|
||||
|
||||
@@ -4,6 +4,6 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AuthRequest {
|
||||
private String email;
|
||||
private String login;
|
||||
private String password;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.krylov.refound.dto;
|
||||
|
||||
import com.krylov.refound.entity.User;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -8,4 +9,5 @@ import lombok.Data;
|
||||
public class AuthResponse {
|
||||
|
||||
private String token;
|
||||
private User user;
|
||||
}
|
||||
|
||||
10
src/main/java/com/krylov/refound/dto/RegisterRequest.java
Normal file
10
src/main/java/com/krylov/refound/dto/RegisterRequest.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.krylov.refound.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RegisterRequest {
|
||||
private String name;
|
||||
private String login;
|
||||
private String password;
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.krylov.refound.entity;
|
||||
|
||||
import com.krylov.refound.enums.Role;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.Table;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Getter;
|
||||
@@ -18,10 +22,17 @@ public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
@Column(unique = true)
|
||||
private String email;
|
||||
private String login;
|
||||
private String password;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private String email;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Role role = Role.USER;
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
this.createdAt = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
|
||||
5
src/main/java/com/krylov/refound/enums/Role.java
Normal file
5
src/main/java/com/krylov/refound/enums/Role.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.krylov.refound.enums;
|
||||
|
||||
public enum Role {
|
||||
ADMIN, USER
|
||||
}
|
||||
@@ -11,6 +11,6 @@ public interface PostMapper {
|
||||
|
||||
Post toEntity(PostRequest request);
|
||||
|
||||
@Mapping(source = "user.email", target = "userEmail")
|
||||
@Mapping(source = "user.login", target = "userEmail")
|
||||
PostResponse toResponse(Post post);
|
||||
}
|
||||
|
||||
@@ -5,5 +5,7 @@ import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
Optional<User> findByEmail(String email);
|
||||
Optional<User> findByLogin(String email);
|
||||
|
||||
Boolean existsByLogin(String login);
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ public class MessageService {
|
||||
: m.getSender().getId();
|
||||
|
||||
String email = isMeSender
|
||||
? m.getReceiver().getEmail()
|
||||
: m.getSender().getEmail();
|
||||
? m.getReceiver().getLogin()
|
||||
: m.getSender().getLogin();
|
||||
|
||||
int unread = messageRepository.countUnread(userId, otherUserId);
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ public class UserService {
|
||||
|
||||
public User getCurrentUser() {
|
||||
|
||||
return repository.findByEmail("moscow_city231@rambler.ru")
|
||||
return repository.findByLogin("moscow_city231@rambler.ru")
|
||||
.orElseThrow(() -> new ApiException(
|
||||
ErrorCode.NOT_FOUND,
|
||||
"User not found",
|
||||
|
||||
11
src/main/resources/db/changelog/add/011-add-name-user.yaml
Normal file
11
src/main/resources/db/changelog/add/011-add-name-user.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 011-add-name-column
|
||||
author: you
|
||||
changes:
|
||||
- addColumn:
|
||||
tableName: users
|
||||
columns:
|
||||
- column:
|
||||
name: name
|
||||
type: VARCHAR(255)
|
||||
10
src/main/resources/db/changelog/add/012-add-email-role.yaml
Normal file
10
src/main/resources/db/changelog/add/012-add-email-role.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 012-add-email-role
|
||||
author: you
|
||||
changes:
|
||||
- addColumn:
|
||||
tableName: users
|
||||
columns:
|
||||
- column: { name: role, type: VARCHAR(255) }
|
||||
- column: { name: email, type: VARCHAR(255) }
|
||||
10
src/main/resources/db/changelog/alter/010-alter-user.yaml
Normal file
10
src/main/resources/db/changelog/alter/010-alter-user.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 010-rename-email-to-login
|
||||
author: you
|
||||
changes:
|
||||
- renameColumn:
|
||||
tableName: users
|
||||
oldColumnName: email
|
||||
newColumnName: login
|
||||
columnDataType: VARCHAR(255)
|
||||
@@ -8,3 +8,6 @@ databaseChangeLog:
|
||||
- include: { file: db/changelog/constraint/007-fk-post-user-constraint.yaml }
|
||||
- include: { file: db/changelog/constraint/008-fk-message-user.yaml }
|
||||
- include: { file: db/changelog/constraint/009-fk-favorite-user.yaml }
|
||||
- include: { file: db/changelog/alter/010-alter-user.yaml }
|
||||
- include: { file: db/changelog/add/011-add-name-user.yaml }
|
||||
- include: { file: db/changelog/add/012-add-email-role.yaml}
|
||||
Reference in New Issue
Block a user