diff --git a/build.gradle b/build.gradle index 6da0afa..4d4e3f8 100644 --- a/build.gradle +++ b/build.gradle @@ -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') { diff --git a/src/main/java/com/android/mobileappserver/Mail/MailController.java b/src/main/java/com/android/mobileappserver/Mail/MailController.java new file mode 100644 index 0000000..111fea6 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Mail/MailController.java @@ -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 getAll(@RequestParam("page") int page, + @RequestParam("size") int size) { + return mailService.getAllMailPaged(page, size).stream().map( + MailDTO::new + ).toList(); + } +} diff --git a/src/main/java/com/android/mobileappserver/Mail/MailDTO.java b/src/main/java/com/android/mobileappserver/Mail/MailDTO.java new file mode 100644 index 0000000..e0fb9d8 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Mail/MailDTO.java @@ -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(); + } +} diff --git a/src/main/java/com/android/mobileappserver/Mail/MailModel.java b/src/main/java/com/android/mobileappserver/Mail/MailModel.java new file mode 100644 index 0000000..1de35ce --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Mail/MailModel.java @@ -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; + } +} diff --git a/src/main/java/com/android/mobileappserver/Mail/MailRepository.java b/src/main/java/com/android/mobileappserver/Mail/MailRepository.java new file mode 100644 index 0000000..36acbe3 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Mail/MailRepository.java @@ -0,0 +1,6 @@ +package com.android.mobileappserver.Mail; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MailRepository extends JpaRepository { +} diff --git a/src/main/java/com/android/mobileappserver/Mail/MailService.java b/src/main/java/com/android/mobileappserver/Mail/MailService.java new file mode 100644 index 0000000..2b42ad5 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Mail/MailService.java @@ -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 getAllMailPaged(int page, int size){ + return mailRepository.findAll(PageRequest.of(page - 1, size)); + } +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryController.java b/src/main/java/com/android/mobileappserver/Story/StoryController.java new file mode 100644 index 0000000..a948cba --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Story/StoryController.java @@ -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 getAll(@RequestParam("page") int page, + @RequestParam("size") int size) { + return storyService.getAllStoryPaged(page, size).stream().map( + StoryDTO::new + ).toList(); + } +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryDTO.java b/src/main/java/com/android/mobileappserver/Story/StoryDTO.java new file mode 100644 index 0000000..d331f7e --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Story/StoryDTO.java @@ -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(); + } +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryModel.java b/src/main/java/com/android/mobileappserver/Story/StoryModel.java new file mode 100644 index 0000000..0877a1d --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Story/StoryModel.java @@ -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; + } +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryRepository.java b/src/main/java/com/android/mobileappserver/Story/StoryRepository.java new file mode 100644 index 0000000..10218ea --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Story/StoryRepository.java @@ -0,0 +1,6 @@ +package com.android.mobileappserver.Story; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface StoryRepository extends JpaRepository { +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryService.java b/src/main/java/com/android/mobileappserver/Story/StoryService.java new file mode 100644 index 0000000..6e4d864 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Story/StoryService.java @@ -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 getAllStoryPaged(int page, int size){ + return storyRepository.findAll(PageRequest.of(page - 1, size)); + } +} diff --git a/src/main/java/com/android/mobileappserver/User/UserController.java b/src/main/java/com/android/mobileappserver/User/UserController.java new file mode 100644 index 0000000..2216e34 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserController.java @@ -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 getUserStories(@PathVariable("id") Long id){ + return userService.getUserStories(id).stream() + .map(StoryDTO::new) + .toList(); + } + +} diff --git a/src/main/java/com/android/mobileappserver/User/UserDTO.java b/src/main/java/com/android/mobileappserver/User/UserDTO.java new file mode 100644 index 0000000..2b437a2 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserDTO.java @@ -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(); + } +} diff --git a/src/main/java/com/android/mobileappserver/User/UserModel.java b/src/main/java/com/android/mobileappserver/User/UserModel.java new file mode 100644 index 0000000..aa0b958 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserModel.java @@ -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 stories; + + @OneToMany(fetch = FetchType.EAGER, mappedBy = "user") + private List mails; + + public UserModel(String login, String email, String password, byte[] photo) { + this.login = login; + this.email = email; + this.password = password; + this.photo = photo; + } +} diff --git a/src/main/java/com/android/mobileappserver/User/UserRepository.java b/src/main/java/com/android/mobileappserver/User/UserRepository.java new file mode 100644 index 0000000..66527c6 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserRepository.java @@ -0,0 +1,7 @@ +package com.android.mobileappserver.User; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + UserModel findByLogin(String login); +} diff --git a/src/main/java/com/android/mobileappserver/User/UserService.java b/src/main/java/com/android/mobileappserver/User/UserService.java new file mode 100644 index 0000000..aacc8eb --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserService.java @@ -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 getUserStories(Long id){ + UserModel user = userRepository.getReferenceById(id); + return user.getStories(); + } + + @Transactional + public UserModel getUserById(Long id){ + return userRepository.getReferenceById(id); + } +} diff --git a/src/main/java/com/android/mobileappserver/User/UserSignInDTO.java b/src/main/java/com/android/mobileappserver/User/UserSignInDTO.java new file mode 100644 index 0000000..875810c --- /dev/null +++ b/src/main/java/com/android/mobileappserver/User/UserSignInDTO.java @@ -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; +} diff --git a/src/main/java/com/android/mobileappserver/WebConfiguration.java b/src/main/java/com/android/mobileappserver/WebConfiguration.java new file mode 100644 index 0000000..7da3c98 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/WebConfiguration.java @@ -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("*"); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..433e137 --- /dev/null +++ b/src/main/resources/application.yml @@ -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