Сдал лабу
This commit is contained in:
parent
3f433ac53b
commit
1e86222db0
@ -0,0 +1,7 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
public class MailNotFoundException extends RuntimeException{
|
||||
public MailNotFoundException(Long id) {
|
||||
super(String.format("Mail with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,11 +1,15 @@
|
||||
package com.android.mobileappserver.Mail;
|
||||
|
||||
import com.android.mobileappserver.Story.StoryModel;
|
||||
import com.android.mobileappserver.Story.StoryNotFoundException;
|
||||
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;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MailService {
|
||||
private final UserService userService;
|
||||
@ -43,7 +47,8 @@ public class MailService {
|
||||
|
||||
@Transactional
|
||||
public MailModel getById(Long id){
|
||||
return mailRepository.getReferenceById(id);
|
||||
final Optional<MailModel> mail = mailRepository.findById(id);
|
||||
return mail.orElseThrow(() -> new MailNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -40,4 +40,13 @@ public class StoryController {
|
||||
StoryDTO::new
|
||||
).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/getByUser")
|
||||
public List<StoryDTO> getByUser(@RequestParam("page") int page,
|
||||
@RequestParam("size") int size,
|
||||
@RequestParam("userId") Long userId) {
|
||||
return storyService.getUserStoryPaged(userId, page, size).stream().map(
|
||||
StoryDTO::new
|
||||
).toList();
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,15 @@ package com.android.mobileappserver.Story;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class StoryDTO {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String description;
|
||||
private byte[] cover;
|
||||
private String cover;
|
||||
private Long postdate;
|
||||
private Long userId;
|
||||
|
||||
@ -17,8 +19,8 @@ public class StoryDTO {
|
||||
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();
|
||||
this.cover = new String(story.getCover(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
public class StoryNotFoundException extends RuntimeException{
|
||||
public StoryNotFoundException(Long id) {
|
||||
super(String.format("Story with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package com.android.mobileappserver.Story;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StoryRepository extends JpaRepository<StoryModel, Long> {
|
||||
Page<StoryModel> findAllByUserId(Long userIdб, Pageable pageable);
|
||||
}
|
||||
|
@ -6,6 +6,9 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class StoryService {
|
||||
private final UserService userService;
|
||||
@ -16,15 +19,20 @@ public class StoryService {
|
||||
this.storyRepository = storyRepository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Transactional
|
||||
public StoryModel getById(Long id){
|
||||
return storyRepository.getReferenceById(id);
|
||||
final Optional<StoryModel> story = storyRepository.findById(id);
|
||||
return story.orElseThrow(() -> new StoryNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public StoryModel insert(StoryDTO storyDTO){
|
||||
var user = userService.getUserById(storyDTO.getUserId());
|
||||
StoryModel story = new StoryModel(storyDTO.getTitle(), storyDTO.getDescription(), storyDTO.getCover(), user);
|
||||
StoryModel story = new StoryModel(
|
||||
storyDTO.getTitle(),
|
||||
storyDTO.getDescription(),
|
||||
storyDTO.getCover().getBytes(StandardCharsets.UTF_8),
|
||||
user);
|
||||
return storyRepository.save(story);
|
||||
}
|
||||
|
||||
@ -33,7 +41,7 @@ public class StoryService {
|
||||
final StoryModel story = getById(storyDTO.getId());
|
||||
story.setTitle(storyDTO.getTitle());
|
||||
story.setDescription(storyDTO.getDescription());
|
||||
story.setCover(storyDTO.getCover());
|
||||
story.setCover(storyDTO.getCover().getBytes(StandardCharsets.UTF_8));
|
||||
return storyRepository.save(story);
|
||||
}
|
||||
|
||||
@ -51,4 +59,9 @@ public class StoryService {
|
||||
public Page<StoryModel> getAllStoryPaged(int page, int size){
|
||||
return storyRepository.findAll(PageRequest.of(page - 1, size));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Page<StoryModel> getUserStoryPaged(Long userId, int page, int size){
|
||||
return storyRepository.findAllByUserId(userId, PageRequest.of(page - 1, size));
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,11 @@ public class UserController {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public UserDTO get(@PathVariable("id") Long id){
|
||||
return new UserDTO(userService.getUserById(id));
|
||||
}
|
||||
|
||||
@PostMapping("/signin")
|
||||
public UserDTO signIn(@RequestBody UserSignInDTO signInDTO){
|
||||
return new UserDTO(userService.signIn(signInDTO));
|
||||
@ -38,4 +43,10 @@ public class UserController {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/getAll")
|
||||
public List<UserDTO> getAll() {
|
||||
return userService.getAll().stream().map(
|
||||
UserDTO::new
|
||||
).toList();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package com.android.mobileappserver.User;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class UserDTO {
|
||||
@ -10,13 +12,13 @@ public class UserDTO {
|
||||
private String login;
|
||||
private String password;
|
||||
private String email;
|
||||
private byte[] photo;
|
||||
private String 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();
|
||||
this.photo = user.getPhoto() != null ? new String(user.getPhoto(), StandardCharsets.UTF_8) : null;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
public class UserNotFoundException extends RuntimeException{
|
||||
public UserNotFoundException(Long id) {
|
||||
super(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
package com.android.mobileappserver.User;
|
||||
|
||||
import com.android.mobileappserver.Story.StoryModel;
|
||||
import com.android.mobileappserver.Story.StoryNotFoundException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@ -17,8 +22,13 @@ public class UserService {
|
||||
|
||||
@Transactional
|
||||
public UserModel update(UserDTO user){
|
||||
UserModel userCreate = new UserModel(user.getLogin(), user.getEmail(), user.getPassword(), user.getPhoto());
|
||||
return userRepository.save(userCreate);
|
||||
final UserModel currentUser = getUserById(user.getId());
|
||||
byte[] photoBytes = user.getPhoto() != null ? user.getPhoto().getBytes(StandardCharsets.UTF_8) : null;
|
||||
currentUser.setLogin(user.getLogin());
|
||||
currentUser.setEmail(user.getEmail());
|
||||
currentUser.setPassword(user.getPassword());
|
||||
currentUser.setPhoto(photoBytes);
|
||||
return userRepository.save(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -32,7 +42,12 @@ public class UserService {
|
||||
|
||||
@Transactional
|
||||
public UserModel signUp(UserDTO user){
|
||||
UserModel userCreate = new UserModel(user.getLogin(), user.getEmail(), user.getPassword(), user.getPhoto());
|
||||
byte[] photoBytes = user.getPhoto() != null ? user.getPhoto().getBytes(StandardCharsets.UTF_8) : null;
|
||||
UserModel userCreate = new UserModel(
|
||||
user.getLogin(),
|
||||
user.getEmail(),
|
||||
user.getPassword(),
|
||||
photoBytes);
|
||||
return userRepository.save(userCreate);
|
||||
}
|
||||
|
||||
@ -44,6 +59,12 @@ public class UserService {
|
||||
|
||||
@Transactional
|
||||
public UserModel getUserById(Long id){
|
||||
return userRepository.getReferenceById(id);
|
||||
final Optional<UserModel> user = userRepository.findById(id);
|
||||
return user.orElseThrow(() -> new UserNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<UserModel> getAll(){
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user