added User class
This commit is contained in:
parent
6e2c298f08
commit
995c69a71b
@ -0,0 +1,20 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HomeController {
|
||||||
|
@RequestMapping("/")
|
||||||
|
public String greet(){
|
||||||
|
return "Server started";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public String lol(){
|
||||||
|
return "Server test???!?!??!?!";
|
||||||
|
}
|
||||||
|
public void test(){
|
||||||
|
System.out.println("AAAAAAAAaa");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.controller;
|
||||||
|
|
||||||
|
import dev.ksan.etfoglasiserver.model.User;
|
||||||
|
import dev.ksan.etfoglasiserver.service.UserService;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
@Autowired UserService service;
|
||||||
|
|
||||||
|
@GetMapping("/users")
|
||||||
|
public List<User> getUsers() {
|
||||||
|
return service.getUsers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/users/{userId}")
|
||||||
|
public User getUserById(@PathVariable UUID userId) {
|
||||||
|
return service.getUserById(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/users")
|
||||||
|
public void addUser(@RequestBody User user) {
|
||||||
|
service.addUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/users")
|
||||||
|
public void updateUser(@RequestBody User user) {
|
||||||
|
service.updateUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/users/{userId}")
|
||||||
|
public void deleteUser(@PathVariable UUID userId) {
|
||||||
|
service.deleteUser(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/dev/ksan/etfoglasiserver/dto/UserDTO.java
Normal file
25
src/main/java/dev/ksan/etfoglasiserver/dto/UserDTO.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.dto;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UserDTO {
|
||||||
|
private UUID id;
|
||||||
|
private String email;
|
||||||
|
public UserDTO() {}
|
||||||
|
public UserDTO(UUID id, String email) {
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
public UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
}
|
||||||
78
src/main/java/dev/ksan/etfoglasiserver/model/Entry.java
Normal file
78
src/main/java/dev/ksan/etfoglasiserver/model/Entry.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Entry {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column private long subjectId;
|
||||||
|
private String title;
|
||||||
|
private String date;
|
||||||
|
private String info;
|
||||||
|
private String paragraph;
|
||||||
|
|
||||||
|
public Entry() {}
|
||||||
|
public Entry(String title, String date, String info, List<String> paragraphs) {
|
||||||
|
this.title = title;
|
||||||
|
this.date = date;
|
||||||
|
this.info = info;
|
||||||
|
|
||||||
|
this.paragraph = String.join("\n", paragraph);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entry(String title, String date, String info, String paragraph) {
|
||||||
|
this.title = title;
|
||||||
|
this.date = date;
|
||||||
|
this.info = info;
|
||||||
|
this.paragraph = paragraph;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getParagraph() {
|
||||||
|
return paragraph;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParagraph(List<String> paragraphs) {
|
||||||
|
this.paragraph = String.join("\n", paragraphs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParagraph(String paragraph) {
|
||||||
|
this.paragraph = paragraph;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInfo() {
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null) return false;
|
||||||
|
Entry subject = (Entry) o;
|
||||||
|
if (title.equals(subject.getTitle())
|
||||||
|
&& date.equals(subject.getDate())
|
||||||
|
&& info.equals(subject.getInfo())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return title + " " + date + " " + info + "\n\t" + paragraph + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,29 +1,35 @@
|
|||||||
package dev.ksan.etfoglasiserver.model;
|
package dev.ksan.etfoglasiserver.model;
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import java.time.LocalDateTime;
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private UUID id;
|
private UUID id;
|
||||||
|
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true, length = 255)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false, length = 255)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@Column(nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
|
||||||
|
private LocalDateTime regTime;
|
||||||
|
|
||||||
public UUID getId() {
|
public UUID getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
@ -39,4 +45,8 @@ public class User {
|
|||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getRegTime() {
|
||||||
|
return regTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,16 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.repository;
|
||||||
|
|
||||||
|
import dev.ksan.etfoglasiserver.model.User;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepo extends JpaRepository<User, UUID> {
|
||||||
|
|
||||||
|
@Query("SELECT u FROM User u WHERE u.email = :email")
|
||||||
|
Optional<User> findByEmail(String email);
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
package dev.ksan.etfoglasiserver.service;
|
||||||
|
|
||||||
|
import dev.ksan.etfoglasiserver.dto.UserDTO;
|
||||||
|
import dev.ksan.etfoglasiserver.model.User;
|
||||||
|
import dev.ksan.etfoglasiserver.repository.UserRepo;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
@Autowired UserRepo userRepo;
|
||||||
|
/*
|
||||||
|
public List<UserDTO> getUsers() {
|
||||||
|
return userRepo.findAll().stream().map(this::toDTO).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO getUserById(UUID id) {
|
||||||
|
return userRepo.findById(id).map(this::toDTO).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO toDTO(User user) {
|
||||||
|
return new UserDTO(user.getId(), user.getEmail());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public List<User> getUsers() {
|
||||||
|
System.out.println("getUsers");
|
||||||
|
return userRepo.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUserById(UUID userId) {
|
||||||
|
return userRepo.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addUser(User prod) {
|
||||||
|
userRepo.save(prod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addNewUser(UserDTO userDTO) {
|
||||||
|
Optional<User> user = userRepo.findByEmail(userDTO.getEmail());
|
||||||
|
}
|
||||||
|
public void updateUser(User prod) {
|
||||||
|
userRepo.save(prod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteUser(UUID userId) {
|
||||||
|
userRepo.deleteById(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user