diff --git a/backend/src/main/java/com/example/backend/users/api/UserController.java b/backend/src/main/java/com/example/backend/users/api/UserController.java index 532744c..608dc0e 100644 --- a/backend/src/main/java/com/example/backend/users/api/UserController.java +++ b/backend/src/main/java/com/example/backend/users/api/UserController.java @@ -1,61 +1,61 @@ -// package com.example.backend.users.api; +package com.example.backend.users.api; -// import org.modelmapper.ModelMapper; -// import org.springframework.stereotype.Controller; -// import org.springframework.ui.Model; -// import org.springframework.validation.BindingResult; -// import org.springframework.web.bind.annotation.GetMapping; -// import org.springframework.web.bind.annotation.ModelAttribute; -// import org.springframework.web.bind.annotation.PostMapping; -// import org.springframework.web.bind.annotation.RequestMapping; -// import org.springframework.web.bind.annotation.RestController; +import org.modelmapper.ModelMapper; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -// import com.example.backend.core.configurations.Constants; -// import com.example.backend.users.model.UserEntity; -// import com.example.backend.users.service.UserService; +import com.example.backend.core.configurations.Constants; +import com.example.backend.users.model.UserEntity; +import com.example.backend.users.service.UserService; -// import jakarta.validation.Valid; -// import org.springframework.web.bind.annotation.RequestBody; +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.RequestBody; -// @Controller -// // @RequestMapping(Constants.API_URL + "/user") -// public class UserController { +@Controller +// @RequestMapping(Constants.API_URL + "/user") +public class UserController { -// private static final String CATEGORIES_VIEW = "categories"; -// private static final String LOGIN_VIEW = "login"; -// private static final String USER_ATTRIBUTE = "user"; + private static final String CATEGORIES_VIEW = "categories"; + private static final String LOGIN_VIEW = "login"; + private static final String USER_ATTRIBUTE = "user"; -// private final UserService userService; -// private final ModelMapper modelMapper; + private final UserService userService; + private final ModelMapper modelMapper; -// public UserController(UserService userService, ModelMapper modelMapper) { -// this.modelMapper = modelMapper; -// this.userService = userService; -// } + public UserController(UserService userService, ModelMapper modelMapper) { + this.modelMapper = modelMapper; + this.userService = userService; + } -// private UserEntity toEntity(UserDTO dto) { -// return modelMapper.map(dto, UserEntity.class); -// } + private UserEntity toEntity(UserDTO dto) { + return modelMapper.map(dto, UserEntity.class); + } -// private UserDTO toDto(UserEntity entity) { -// return modelMapper.map(entity, UserDTO.class); -// } + private UserDTO toDto(UserEntity entity) { + return modelMapper.map(entity, UserDTO.class); + } -// @GetMapping("/edit/") -// public String create(Model model) { -// model.addAttribute(CATEGORIES_VIEW, new UserDTO()); -// return LOGIN_VIEW; -// } + @GetMapping("/edit/") + public String create(Model model) { + model.addAttribute(CATEGORIES_VIEW, new UserDTO()); + return LOGIN_VIEW; + } -// @PostMapping("/edit/") -// public String create( -// @ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDTO userDTO, -// BindingResult bindingResult, -// Model model) { -// if (bindingResult.hasErrors()) { -// return LOGIN_VIEW + "/login"; -// } -// userService.create(toEntity(userDTO)); -// return Constants.REDIRECT_VIEW + "/user"; -// } -// } + @PostMapping("/edit/") + public String create( + @ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDTO userDTO, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return LOGIN_VIEW + "/login"; + } + userService.create(toEntity(userDTO)); + return Constants.REDIRECT_VIEW + "/user"; + } +} diff --git a/backend/src/main/java/com/example/backend/users/repository/UserRepository.java b/backend/src/main/java/com/example/backend/users/repository/UserRepository.java index 34bf1c4..fd63fff 100644 --- a/backend/src/main/java/com/example/backend/users/repository/UserRepository.java +++ b/backend/src/main/java/com/example/backend/users/repository/UserRepository.java @@ -2,9 +2,11 @@ package com.example.backend.users.repository; import java.util.Optional; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; import com.example.backend.users.model.UserEntity; -public interface UserRepository extends CrudRepository { +public interface UserRepository + extends CrudRepository, PagingAndSortingRepository { Optional findByLoginIgnoreCase(String login); } diff --git a/backend/src/main/resources/templates/default.html b/backend/src/main/resources/templates/default.html index 0ecb274..eefb856 100644 --- a/backend/src/main/resources/templates/default.html +++ b/backend/src/main/resources/templates/default.html @@ -32,18 +32,28 @@