Остался контроллер User, смена Service

This commit is contained in:
Ismailov_Rovshan 2023-05-12 18:08:15 +04:00
parent c8f79a0d98
commit 72166d3a35
12 changed files with 274 additions and 2 deletions

View File

@ -0,0 +1,43 @@
package ru.ulstu.is.sbapp.socialNetwork.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.socialNetwork.dto.CommunityDTO;
import ru.ulstu.is.sbapp.socialNetwork.services.CommunityService;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/communities")
public class CommunityController {
private final CommunityService communityService;
public CommunityController(CommunityService communityService) {
this.communityService = communityService;
}
@GetMapping("/{id}")
public CommunityDTO getCommunity(@PathVariable Long id) {
return new CommunityDTO(communityService.findCommunity(id));
}
@GetMapping("")
public List<CommunityDTO> getCommunities() {
return communityService.findAllCommunitys().stream().map(CommunityDTO::new).toList();
}
@PostMapping("")
public CommunityDTO createCommunity(@RequestBody @Valid CommunityDTO community) {
return new CommunityDTO(communityService.addCommunity(community.getName()));
}
@PatchMapping("")
public CommunityDTO updateCommunity(@PathVariable Long id, @RequestBody @Valid CommunityDTO community) {
return new CommunityDTO(communityService.updateCommunity(id, community.getName()));
}
@DeleteMapping("/{id}")
public CommunityDTO deleteCommunity(@PathVariable Long id) {
return new CommunityDTO(communityService.deleteCommunity(id));
}
}

View File

@ -0,0 +1,44 @@
package ru.ulstu.is.sbapp.socialNetwork.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.socialNetwork.dto.MusicDTO;
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
import ru.ulstu.is.sbapp.socialNetwork.services.MusicService;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/musics")
public class MusicController {
private final MusicService musicService;
public MusicController(MusicService musicService){
this.musicService = musicService;
}
@GetMapping("/{id}")
public MusicDTO getMusic(@PathVariable Long id){
return new MusicDTO(musicService.findMusic(id));
}
@GetMapping("")
public List<MusicDTO> getMusics() {
return musicService.findAllMusics().stream().map(MusicDTO::new).toList();
}
@PostMapping("")
public MusicDTO createMusic(@RequestBody @Valid MusicDTO music) {
return new MusicDTO(musicService.addMusic(music.getName()));
}
@PatchMapping("/{id}")
public MusicDTO updateMusic(@PathVariable Long id,
@RequestBody @Valid MusicDTO music) {
return new MusicDTO(musicService.updateMusic(id, music.getName()));
}
@DeleteMapping("/{id}")
public MusicDTO deleteMusic(@PathVariable Long id) {
return new MusicDTO(musicService.deleteMusic(id));
}
}

View File

@ -0,0 +1,32 @@
package ru.ulstu.is.sbapp.socialNetwork.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.socialNetwork.dto.UserDTO;
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
import ru.ulstu.is.sbapp.socialNetwork.services.UserService;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService){
this.userService = userService;
}
@GetMapping("/{id}")
public UserDTO getUser(@PathVariable Long id) {
return new UserDTO(userService.findUser(id));
}
@GetMapping("")
public List<UserDTO> getUsers() {
return userService.findAllUsers().stream().map(UserDTO::new).toList();
}
}

View File

@ -0,0 +1,43 @@
package ru.ulstu.is.sbapp.socialNetwork.dto;
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
import java.util.List;
public class CommunityDTO {
private Long id;
private String name;
public CommunityDTO(){
}
public CommunityDTO(Community community){
this.id = community.getId();
this.name = community.getName();
}
private List<UserModel> users;
public List<UserModel> getUsers() {
return users;
}
public Long getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

View File

@ -0,0 +1,27 @@
package ru.ulstu.is.sbapp.socialNetwork.dto;
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
public class MusicDTO {
private Long id;
public String name;
public MusicDTO(){
}
public MusicDTO(Music music){
this.id = music.getId();
this.name = music.getName();
}
public Long getId() {return id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@Override
public String toString(){
return "Music{" +
"id = " + id +
"name = " + name + '\'' +
'}';
}
}

View File

@ -0,0 +1,58 @@
package ru.ulstu.is.sbapp.socialNetwork.dto;
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
import java.util.List;
public class UserDTO {
private Long id;
private String name;
private String city;
public UserDTO(){}
public UserDTO(UserModel user){
this.id = user.getId();
this.name = user.getName();
this.city = user.getCity();
}
private List<Music> musics;
private List<Community> communities;
public Long getId(){return id;}
public String getName(){return name;}
public void setName(String firstName){this.name = firstName;}
public void addMusic(Music g) {
musics.add(g);
}
public List<Music> getMusics() {
return musics;
}
public void addCommunity(Community g) {
communities.add(g);
}
public List<Community> getGroups() {
return communities;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return city;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}

View File

@ -38,7 +38,6 @@ public class Community {
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "User{" +

View File

@ -46,7 +46,6 @@ public class UserModel {
public String getName(){return name;}
public void setName(String firstName){this.name = firstName;}
public void addMusic(Music g) {
musics.add(g);
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.is.sbapp.socialNetwork.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
public interface CommunityRepository extends JpaRepository<Community, Long> {
Community findByName(String name);
}

View File

@ -0,0 +1,10 @@
package ru.ulstu.is.sbapp.socialNetwork.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.sbapp.socialNetwork.models.Community;
import ru.ulstu.is.sbapp.socialNetwork.models.Music;
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
public interface MusicRepository extends JpaRepository<Music, Long> {
Music findByName(String name);
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.is.sbapp.socialNetwork.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.sbapp.socialNetwork.models.UserModel;
public interface UserRepository extends JpaRepository<UserModel, Long> {
}

View File

@ -12,6 +12,7 @@ import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
@ -94,6 +95,7 @@ public class UserService {
return em.merge(currentUser);
}
@Transactional
public UserModel deleteUser(Long id) {
final UserModel currentUser = findUser(id);