Сервер написан
This commit is contained in:
parent
cae887a639
commit
3f433ac53b
@ -29,6 +29,7 @@ dependencies {
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("api/mail")
|
||||
public class MailController {
|
||||
private final MailService mailService;
|
||||
|
||||
public MailController(MailService mailService){
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
@PostMapping({"/create"})
|
||||
public MailDTO create(@RequestBody MailDTO mailDTO) {
|
||||
return new MailDTO(mailService.insert(mailDTO));
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public MailDTO update(@PathVariable("id") Long id, @RequestBody MailDTO mailDTO){
|
||||
return new MailDTO(mailService.update(mailDTO));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void delete(@PathVariable("id") Long id) throws Exception{
|
||||
mailService.delete(id);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public MailDTO get(@PathVariable("id") Long id){
|
||||
return new MailDTO(mailService.getById(id));
|
||||
}
|
||||
|
||||
@GetMapping("/getAll")
|
||||
public List<MailDTO> getAll(@RequestParam("page") int page,
|
||||
@RequestParam("size") int size) {
|
||||
return mailService.getAllMailPaged(page, size).stream().map(
|
||||
MailDTO::new
|
||||
).toList();
|
||||
}
|
||||
}
|
20
src/main/java/com/android/mobileappserver/Mail/MailDTO.java
Normal file
20
src/main/java/com/android/mobileappserver/Mail/MailDTO.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class MailDTO {
|
||||
private Long id;
|
||||
private String message;
|
||||
private Long postdate;
|
||||
private Long userId;
|
||||
|
||||
public MailDTO(MailModel mail){
|
||||
this.id = mail.getId();
|
||||
this.message = mail.getMessage();
|
||||
this.postdate = mail.getPostdate();
|
||||
this.userId = mail.getUser().getId();
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import com.android.mobileappserver.User.UserModel;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name="_mail")
|
||||
public class MailModel {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String message;
|
||||
private Long postdate;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "user_id")
|
||||
private UserModel user;
|
||||
|
||||
public MailModel(String message, UserModel user){
|
||||
this.message = message;
|
||||
this.postdate = new Date().getTime();
|
||||
this.user = user;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface MailRepository extends JpaRepository<MailModel, Long> {
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import com.android.mobileappserver.User.UserService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class MailService {
|
||||
private final UserService userService;
|
||||
private final MailRepository mailRepository;
|
||||
|
||||
public MailService(UserService userService, MailRepository mailRepository){
|
||||
this.userService = userService;
|
||||
this.mailRepository = mailRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public MailModel insert(MailDTO mailDTO) {
|
||||
var user = userService.getUserById(mailDTO.getUserId());
|
||||
MailModel mail = new MailModel(mailDTO.getMessage(), user);
|
||||
return mailRepository.save(mail);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public MailModel update(MailDTO mailDTO){
|
||||
final MailModel mail = getById(mailDTO.getId());
|
||||
mail.setMessage(mailDTO.getMessage());
|
||||
return mailRepository.save(mail);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long id) throws Exception{
|
||||
try{
|
||||
final MailModel curMail = getById(id);
|
||||
mailRepository.delete(curMail);
|
||||
}catch(Exception ex){
|
||||
throw new Exception("Ошибка при удалении mail с id: " + id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public MailModel getById(Long id){
|
||||
return mailRepository.getReferenceById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Page<MailModel> getAllMailPaged(int page, int size){
|
||||
return mailRepository.findAll(PageRequest.of(page - 1, size));
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("api/story")
|
||||
public class StoryController {
|
||||
private final StoryService storyService;
|
||||
|
||||
public StoryController(StoryService storyService) {
|
||||
this.storyService = storyService;
|
||||
}
|
||||
|
||||
@PostMapping({"/create"})
|
||||
public StoryDTO create(@RequestBody StoryDTO storyDTO) {
|
||||
return new StoryDTO(storyService.insert(storyDTO));
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public StoryDTO update(@PathVariable("id") Long id, @RequestBody StoryDTO storyDTO){
|
||||
return new StoryDTO(storyService.update(storyDTO));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void delete(@PathVariable("id") Long id) throws Exception{
|
||||
storyService.delete(id);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public StoryDTO get(@PathVariable("id") Long id){
|
||||
return new StoryDTO(storyService.getById(id));
|
||||
}
|
||||
|
||||
@GetMapping("/getAll")
|
||||
public List<StoryDTO> getAll(@RequestParam("page") int page,
|
||||
@RequestParam("size") int size) {
|
||||
return storyService.getAllStoryPaged(page, size).stream().map(
|
||||
StoryDTO::new
|
||||
).toList();
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class StoryDTO {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String description;
|
||||
private byte[] cover;
|
||||
private Long postdate;
|
||||
private Long userId;
|
||||
|
||||
public StoryDTO(StoryModel story){
|
||||
this.id = story.getId();
|
||||
this.title = story.getTitle();
|
||||
this.description = story.getDescription();
|
||||
this.cover = story.getCover();
|
||||
this.postdate = story.getPostdate();
|
||||
this.userId = story.getUser().getId();
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import com.android.mobileappserver.User.UserModel;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name="_story")
|
||||
public class StoryModel {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String title;
|
||||
private String description;
|
||||
@Lob
|
||||
private byte[] cover;
|
||||
private Long postdate;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "user_id")
|
||||
private UserModel user;
|
||||
|
||||
public StoryModel(String title, String description, byte[] cover, UserModel user){
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.cover = cover;
|
||||
this.postdate = new Date().getTime();
|
||||
this.user = user;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StoryRepository extends JpaRepository<StoryModel, Long> {
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import com.android.mobileappserver.User.UserService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StoryService {
|
||||
private final UserService userService;
|
||||
private final StoryRepository storyRepository;
|
||||
|
||||
public StoryService(UserService userService, StoryRepository storyRepository){
|
||||
this.userService = userService;
|
||||
this.storyRepository = storyRepository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public StoryModel getById(Long id){
|
||||
return storyRepository.getReferenceById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public StoryModel insert(StoryDTO storyDTO){
|
||||
var user = userService.getUserById(storyDTO.getUserId());
|
||||
StoryModel story = new StoryModel(storyDTO.getTitle(), storyDTO.getDescription(), storyDTO.getCover(), user);
|
||||
return storyRepository.save(story);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public StoryModel update(StoryDTO storyDTO){
|
||||
final StoryModel story = getById(storyDTO.getId());
|
||||
story.setTitle(storyDTO.getTitle());
|
||||
story.setDescription(storyDTO.getDescription());
|
||||
story.setCover(storyDTO.getCover());
|
||||
return storyRepository.save(story);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long id) throws Exception{
|
||||
try{
|
||||
final StoryModel curStory = getById(id);
|
||||
storyRepository.delete(curStory);
|
||||
}catch(Exception ex){
|
||||
throw new Exception("Ошибка при удалении story с id: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Page<StoryModel> getAllStoryPaged(int page, int size){
|
||||
return storyRepository.findAll(PageRequest.of(page - 1, size));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import com.android.mobileappserver.Story.StoryDTO;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("api/user")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@PostMapping("/signin")
|
||||
public UserDTO signIn(@RequestBody UserSignInDTO signInDTO){
|
||||
return new UserDTO(userService.signIn(signInDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/signup")
|
||||
public UserDTO signUp(@RequestBody UserDTO userDTO){
|
||||
UserModel user = userService.signUp(userDTO);
|
||||
return new UserDTO(user);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public UserDTO update(@RequestBody UserDTO userDTO){
|
||||
UserModel user = userService.update(userDTO);
|
||||
return new UserDTO(user);
|
||||
}
|
||||
|
||||
@GetMapping("/getstories/{id}")
|
||||
public List<StoryDTO> getUserStories(@PathVariable("id") Long id){
|
||||
return userService.getUserStories(id).stream()
|
||||
.map(StoryDTO::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
}
|
22
src/main/java/com/android/mobileappserver/User/UserDTO.java
Normal file
22
src/main/java/com/android/mobileappserver/User/UserDTO.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class UserDTO {
|
||||
private Long id;
|
||||
private String login;
|
||||
private String password;
|
||||
private String email;
|
||||
private byte[] photo;
|
||||
|
||||
public UserDTO(UserModel user){
|
||||
this.id = user.getId();
|
||||
this.login = user.getLogin();
|
||||
this.password = user.getPassword();
|
||||
this.email = user.getEmail();
|
||||
this.photo = user.getPhoto();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import com.android.mobileappserver.Mail.MailModel;
|
||||
import com.android.mobileappserver.Story.StoryModel;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name="_user")
|
||||
public class UserModel {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String login;
|
||||
private String password;
|
||||
private String email;
|
||||
@Lob
|
||||
private byte[] photo;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
|
||||
private List<StoryModel> stories;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
|
||||
private List<MailModel> mails;
|
||||
|
||||
public UserModel(String login, String email, String password, byte[] photo) {
|
||||
this.login = login;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.photo = photo;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserModel, Long> {
|
||||
UserModel findByLogin(String login);
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import com.android.mobileappserver.Story.StoryModel;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel update(UserDTO user){
|
||||
UserModel userCreate = new UserModel(user.getLogin(), user.getEmail(), user.getPassword(), user.getPhoto());
|
||||
return userRepository.save(userCreate);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel signIn(UserSignInDTO userSignInDTO){
|
||||
UserModel user = userRepository.findByLogin(userSignInDTO.getLogin());
|
||||
if(Objects.equals(user.getPassword(), userSignInDTO.getPassword())){
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel signUp(UserDTO user){
|
||||
UserModel userCreate = new UserModel(user.getLogin(), user.getEmail(), user.getPassword(), user.getPhoto());
|
||||
return userRepository.save(userCreate);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<StoryModel> getUserStories(Long id){
|
||||
UserModel user = userRepository.getReferenceById(id);
|
||||
return user.getStories();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserModel getUserById(Long id){
|
||||
return userRepository.getReferenceById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserSignInDTO {
|
||||
private String login;
|
||||
private String password;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.android.mobileappserver;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**").allowedMethods("*");
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
|
18
src/main/resources/application.yml
Normal file
18
src/main/resources/application.yml
Normal file
@ -0,0 +1,18 @@
|
||||
spring:
|
||||
main:
|
||||
banner-mode: off
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.PostgreSQLDialect
|
||||
"[format_sql]": true
|
||||
jdbc:
|
||||
"[lob.non_contextual_creation]": true
|
||||
show-sql: true
|
||||
datasource:
|
||||
url: jdbc:postgresql://localhost:5432/mdp_db
|
||||
driver-class-name: org.postgresql.Driver
|
||||
username: postgres
|
||||
password: adam200396789
|
Loading…
Reference in New Issue
Block a user