#1 Создала сущность юзера.

Создала класс User, дто UserDTO, репозиторий, сервис, контроллер.
Написала миграцию для создания таблицы users.
Добавила энам UserRole для определения ролей юзеров
parent b8321d49
package com.example.final_exam_l.controller;
import com.example.final_exam_l.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
@Controller
@RequiredArgsConstructor
public class UserController {
private final UserService service;
}
package com.example.final_exam_l.dto;
import com.example.final_exam_l.entity.User;
import lombok.*;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserDTO {
private Integer id;
private String login;
private String email;
private String password;
private String role;
private boolean active;
public static UserDTO from(User user){
return UserDTO.builder()
.id(user.getId())
.login(user.getLogin())
.email(user.getEmail())
.password(user.getPassword())
.role(user.getRole())
.active(user.isActive())
.build();
}
}
package com.example.final_exam_l.entity;
import com.example.final_exam_l.enumiration.UserRole;
import com.sun.istack.NotNull;
import lombok.*;
import javax.persistence.*;
@Table(name = "users")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
@NotNull
private String login;
@Column
@NotNull
private String email;
@Column
@NotNull
private String password;
@Column
@NotNull
@Builder.Default
private String role = UserRole.USER.name();
@Column
@NotNull
@Builder.Default
private boolean active = true;
}
package com.example.final_exam_l.enumiration;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum UserRole {
ADMIN("Админ"), USER("Пользователь");
private String russianName;
}
package com.example.final_exam_l.exception;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@Getter
@Setter
@ResponseStatus(code = HttpStatus.FORBIDDEN)
public class AccessException extends Exception{
private String exception = "Доступ закрыт";
}
package com.example.final_exam_l.exception;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@Getter
@Setter
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private String resource;
private int id;
private String email;
public ResourceNotFoundException(String resource, int id) {
super();
this.resource = resource;
this.id = id;
}
public ResourceNotFoundException(String resource, String email) {
super();
this.resource = resource;
this.email = email;
}
}
package com.example.final_exam_l.exception;
public class StorageException extends RuntimeException{
public StorageException(String message) {
super(message);
}
public StorageException(String message, Throwable cause) {
super(message, cause);
}
}
package com.example.final_exam_l.exception;
public class StorageFileNotFoundException extends StorageException {
public StorageFileNotFoundException(String message) {
super(message);
}
public StorageFileNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
package com.example.final_exam_l.exception;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@NoArgsConstructor
@ResponseStatus(HttpStatus.CONFLICT)
@Getter
public class UserAlreadyExistsException extends RuntimeException {
private String result = "Мейл занят. Введите другой мейл";
}
package com.example.final_exam_l.repository;
import com.example.final_exam_l.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
package com.example.final_exam_l.service;
import com.example.final_exam_l.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository repository;
}
USE `final_exam`;
CREATE Table `users` (
`id` int auto_increment not null,
`login` varchar(128) not null,
`email` varchar(128) not null,
`password` varchar(128) not null,
`active` boolean not null default true,
`role` varchar(24) not null,
primary key (`id`),
unique index `email_unique` (`email` ASC)
);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment