diff --git a/build.gradle b/build.gradle index 482c489..b2eff23 100644 --- a/build.gradle +++ b/build.gradle @@ -14,12 +14,22 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-devtools' + implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' + + implementation 'org.webjars:bootstrap:5.1.3' + implementation 'org.webjars:jquery:3.6.0' + implementation 'org.webjars:font-awesome:6.1.0' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' - implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' - implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final' + implementation 'org.hibernate.validator:hibernate-validator' + + implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' + + testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { diff --git a/data.mv.db b/data.mv.db index cb6c6d5..b42c008 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/front/src/components/catalogs/CatalogUsers.jsx b/front/src/components/catalogs/CatalogUsers.jsx index 2fc4a7b..1e2c5a2 100644 --- a/front/src/components/catalogs/CatalogUsers.jsx +++ b/front/src/components/catalogs/CatalogUsers.jsx @@ -6,7 +6,8 @@ import DataService from "../../services/DataService" let headers = [ {label: "name", text: "Имя"}, {label: "music", text: "Музыка"}, - {label: "fullNames", text: "Группы"} + {label: "community", text: "Группы"}, + { label: "city", text: "Город" } ] export default function CatalogUsers() { const url = "/users"; @@ -14,7 +15,8 @@ export default function CatalogUsers() { const [data, setData] = useState({ name: "", music: [], - fullNames: [] + community: [], + city: "" }); const [selectedData, setSelectedData] = useState({ @@ -58,12 +60,12 @@ export default function CatalogUsers() { } function addCommunity() { - for(let a of data.fullNames) { + for(let a of data.community) { if(a == selectedData.community) return; } - let temp = data.fullNames.slice(0); + let temp = data.community.slice(0); temp.push(selectedData.community); - setData({...data, fullNames: temp}); + setData({...data, community: temp}); } function deleteMusic(e) { @@ -74,12 +76,12 @@ export default function CatalogUsers() { function deleteCommunity(e) { let communityName = e.target.previousSibling.textContent; - let temp = data.fullNames.filter(x => (x != communityName)); - setData({...data, fullNames: temp}) + let temp = data.community.filter(x => (x != communityName)); + setData({...data, community: temp}) } function onAdd() { - setData({name: "", music: [], fullNames: []}); + setData({name: "", music: [], community: []}); } @@ -96,50 +98,72 @@ export default function CatalogUsers() {
- - + +
- +
+ - { - data.music.map(value => ( -
- {value} - -
- )) - } -

- + {data.music.map(value => ( +
+ {value} + +
+ ))} - - { - data.fullNames.map(value => ( -
- {value} - -
- )) - }

- - + + + + {data.community.map(value => ( +
+ {value} + +
+ ))} +

+ +
) } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java index 64d729a..1155aa3 100644 --- a/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java +++ b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java @@ -2,6 +2,8 @@ package ru.ulstu.is.sbapp; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -10,4 +12,18 @@ public class WebConfiguration implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedMethods("*"); } + public static final String REST_API = "/api"; + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("users"); + registry.addViewController("contacts"); + registry.addViewController("catalogs"); + } + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/static/css/**").addResourceLocations("/static/css/"); + } + } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/CommunityController.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/CommunityController.java index caaf422..38dee1a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/CommunityController.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/CommunityController.java @@ -1,6 +1,7 @@ package ru.ulstu.is.sbapp.socialNetwork.controller; import org.springframework.web.bind.annotation.*; +import ru.ulstu.is.sbapp.WebConfiguration; import ru.ulstu.is.sbapp.socialNetwork.dto.CommunityDTO; import ru.ulstu.is.sbapp.socialNetwork.services.CommunityService; @@ -8,7 +9,7 @@ import javax.validation.Valid; import java.util.List; @RestController -@RequestMapping("/communities") +@RequestMapping(WebConfiguration.REST_API + "/communities") public class CommunityController { private final CommunityService communityService; @@ -23,7 +24,7 @@ public class CommunityController { @GetMapping("") public List getCommunities() { - return communityService.findAllCommunitys().stream().map(CommunityDTO::new).toList(); + return communityService.findAllCommunities().stream().map(CommunityDTO::new).toList(); } @PostMapping("") diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/MusicController.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/MusicController.java index 5f7ec61..21e68dc 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/MusicController.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/MusicController.java @@ -2,24 +2,30 @@ package ru.ulstu.is.sbapp.socialNetwork.controller; import org.springframework.web.bind.annotation.*; +import ru.ulstu.is.sbapp.WebConfiguration; 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 org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController -@RequestMapping("/musics") +@RequestMapping(WebConfiguration.REST_API + "/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 getMusics() { return musicService.findAllMusics().stream().map(MusicDTO::new).toList(); diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/UserController.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/UserController.java index f741075..a56a3fb 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/UserController.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/controller/UserController.java @@ -1,15 +1,18 @@ package ru.ulstu.is.sbapp.socialNetwork.controller; import org.springframework.web.bind.annotation.*; +import ru.ulstu.is.sbapp.WebConfiguration; 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") +@RequestMapping(WebConfiguration.REST_API + "/users") public class UserController { private final UserService userService; @@ -27,6 +30,39 @@ public class UserController { return userService.findAllUsers().stream().map(UserDTO::new).toList(); } + @PostMapping("") + public UserDTO createUser(@RequestBody @Valid UserDTO user) { + UserModel result = userService.addUser(user.getName(), user.getCity()); + userService.updateCommunities(result.getId(), user.getCommunity()); + return new UserDTO(userService.updateMusics(result.getId(), user.getMusic())); + } + @PatchMapping("/{id}") + public UserDTO updateUser(@PathVariable Long id, + @RequestBody @Valid UserDTO user) { + UserModel result = userService.updateUser(id, user.getName()); + userService.updateCommunities(result.getId(), user.getCommunity()); + return new UserDTO(userService.updateMusics(result.getId(), user.getMusic())); + } + + @PatchMapping("/add_music/{id}") + public UserDTO addMusic(@PathVariable Long id, @RequestParam Long music_id) { + return new UserDTO(userService.addMusic(id, music_id)); + } + + @PatchMapping("/add_community/{id}") + public UserDTO addCommunity(@PathVariable Long id, @RequestParam Long community_id) { + return new UserDTO(userService.addCommunity(id, community_id)); + } + @GetMapping("/search") + public List searchUsers(@RequestParam("city") String city) { + List users = userService.findUserByCity(city); + return users.stream().map(UserDTO::new).toList(); + } + + @DeleteMapping("/{id}") + public UserDTO deleteUser(@PathVariable Long id) { + return new UserDTO(userService.deleteUser(id)); + } } diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/dto/UserDTO.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/dto/UserDTO.java index aa24f2e..001a166 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/dto/UserDTO.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/dto/UserDTO.java @@ -14,39 +14,61 @@ public class UserDTO { public UserDTO(){} public UserDTO(UserModel user){ + this.id = user.getId(); this.name = user.getName(); this.city = user.getCity(); + if(user.getMusics() != null) { + this.music = user.getMusics().stream().map(Music::getName).toList(); + } + if(user.getGroups() != null) { + this.community = user.getGroups().stream().map(Community::getName).toList(); + } } - private List musics; - private List communities; + public UserDTO(Long id, String name) { + this.id = id; + this.name = name; + } + private List music; + private List community; 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 getMusics() { - return musics; + public List getMusic() { + return music; } - public void addCommunity(Community g) { - communities.add(g); + public List getCommunity() { + return community; } - public List getGroups() { - return communities; + + public void setCommunities(List communities) { + this.community = communities; + } + + public void setMusics(List musics) { + this.music = musics; } public void setCity(String city){ this.city = city; } + public String getCity(){ return city; } + public void setMusic(List music) { + this.music = music; + } + + public void setCommunity(List community) { + this.community = community; + } + @Override public String toString() { return "User{" + @@ -55,4 +77,5 @@ public class UserDTO { ", city='" + city + '\'' + '}'; } + } diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java index 33c96d6..4cd934a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/models/UserModel.java @@ -3,6 +3,7 @@ import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; import javax.persistence.*; +import java.util.ArrayList; import java.util.List; @Entity @@ -35,6 +36,7 @@ public class UserModel { } public UserModel(String name){ this.name = name; + this.city = "Mosscow"; } public UserModel(String name, String city){ this.name = name; @@ -53,6 +55,7 @@ public class UserModel { public void setName(String firstName){this.name = firstName;} public void addMusic(Music g) { + if(musics == null) musics = new ArrayList<>(); musics.add(g); } public List getMusics() { @@ -60,6 +63,7 @@ public class UserModel { } public void addCommunity(Community g) { + if(communities == null) communities = new ArrayList<>(); communities.add(g); } public List getGroups() { diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/repository/UserRepository.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/repository/UserRepository.java index bcc3cc9..af1876a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/repository/UserRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/repository/UserRepository.java @@ -3,5 +3,8 @@ package ru.ulstu.is.sbapp.socialNetwork.repository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.is.sbapp.socialNetwork.models.UserModel; +import java.util.List; + public interface UserRepository extends JpaRepository { + List findByCity(String city); } diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java index 1f00286..9a5b5fe 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/CommunityService.java @@ -40,10 +40,11 @@ public class CommunityService { } @Transactional(readOnly = true) - public List findAllCommunitys() { + public List findAllCommunities() { return repo.findAll(); } + @Transactional public Community updateCommunity(Long id, String name) { if (!StringUtils.hasText(name)) { @@ -79,7 +80,7 @@ public class CommunityService { } @Transactional - public void deleteAllCommunitys() { + public void deleteAllCommunities() { repo.deleteAll(); } diff --git a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java index 6609800..a3facc9 100644 --- a/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java +++ b/src/main/java/ru/ulstu/is/sbapp/socialNetwork/services/UserService.java @@ -9,6 +9,7 @@ import ru.ulstu.is.sbapp.socialNetwork.models.UserModel; import ru.ulstu.is.sbapp.socialNetwork.repository.UserRepository; import javax.persistence.EntityNotFoundException; +import javax.persistence.Query; import java.util.List; import java.util.Optional; @@ -34,12 +35,26 @@ public class UserService { UserModel user = new UserModel(name, city); return repo.save(user); } + @Transactional + public UserModel addUser(String name, String city, List musics, List communities) { + + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Name is null or empty"); + } + + UserModel user = new UserModel(name, city); + UserModel result = repo.save(user); + + updateMusics(result.getId(), musics); + return updateCommunities(user.getId(), communities); + } + @Transactional public UserModel addMusic(Long userId, Long musicId) { final Optional userOpt= repo.findById(userId); if (userOpt.isEmpty()) { - throw new EntityNotFoundException(String.format("Film with id [%s] is not found", musicId)); + throw new EntityNotFoundException(String.format("User with id [%s] is not found", musicId)); } UserModel user = userOpt.get(); @@ -83,11 +98,16 @@ public class UserService { // @Transactional(readOnly = true) // public List findUserByCity(String city){ -// Query query = em.createQuery("select u from UserModel u where u.city = :city", +// Query query = repo.createQuery("select u from UserModel u where u.city = :city", // UserModel.class).setParameter("city", city); // List result = query.getResultList(); -// return result; +// return result; // } + @Transactional(readOnly = true) + public List findUserByCity(String city){ + List result = repo.findByCity(city); + return result; + } @Transactional(readOnly = true) public List findAllUsers() { @@ -112,7 +132,27 @@ public class UserService { return repo.save(currentUser); } //update community, musics + @Transactional + public UserModel updateUser(Long id, String name,String city, List musics, List Communities) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("User name is null or empty"); + } + final Optional currentUserOpt = repo.findById(id); + if(currentUserOpt.isEmpty()) { + return null; + } + + final UserModel currentUser = currentUserOpt.get(); + + currentUser.setName(name); + currentUser.setCity(city); + repo.save(currentUser); + + updateMusics(id, musics); + return updateCommunities(id, Communities); + } + @Transactional public UserModel updateCommunities(Long userId , List communities) { final Optional userOpt = repo.findById(userId); diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java index b5d1d27..b5e3e4e 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java @@ -26,7 +26,7 @@ public class JpaCommunityTest { @Test void testUserCreate() { - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); final Community community = communityService.addCommunity("CodeGPT"); log.info(community.toString()); Assertions.assertNotNull(community.getId()); @@ -34,13 +34,13 @@ public class JpaCommunityTest { @Test void testUserReadNotFound() { - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); Assertions.assertThrows(EntityNotFoundException.class, () -> communityService.findCommunity(-1L)); } @Test void findUser() { - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); final Community community = communityService.addCommunity("CodeGPT"); log.info(community.toString()); final Community findCommunity = communityService.findCommunity(community.getId()); @@ -50,18 +50,18 @@ public class JpaCommunityTest { @Test void testReadAll() { - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); userService.deleteAllUsers(); final Community community = communityService.addCommunity("CodeGPT"); log.info(community.toString()); - final List communitys = communityService.findAllCommunitys(); - Assertions.assertEquals(communitys.size(), 1); + final List communities = communityService.findAllCommunities(); + Assertions.assertEquals(communities.size(), 1); } @Test void updateCommunity() { userService.deleteAllUsers(); - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); final Community community = communityService.addCommunity("CodeGPT 2"); communityService.updateCommunity(community.getId(), "CodeGPT 3"); final Community findCommunity = communityService.findCommunity(community.getId()); @@ -72,7 +72,7 @@ public class JpaCommunityTest { @Test void deleteCommunity() { userService.deleteAllUsers(); - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); final Community community = communityService.addCommunity("CodeGPT"); log.info(community.toString()); communityService.deleteCommunity(community.getId()); @@ -83,7 +83,7 @@ public class JpaCommunityTest { @Test void addUser() { userService.deleteAllUsers(); - communityService.deleteAllCommunitys(); + communityService.deleteAllCommunities(); userService.findAllUsers(); final Community community = communityService.addCommunity("CodeGPT"); final UserModel user = userService.addUser("its_mailov", "Moscow");