diff --git a/data.mv.db b/data.mv.db index ef94f3e..6819e84 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/data.trace.db b/data.trace.db new file mode 100644 index 0000000..3b6df68 --- /dev/null +++ b/data.trace.db @@ -0,0 +1,108 @@ +2024-05-12 15:36:55.285977+04:00 database: wrong user or password; user: "SA" +org.h2.message.DbException: Неверное имя пользователя или пароль +Wrong user name or password [28000-224] + at org.h2.message.DbException.get(DbException.java:223) + at org.h2.message.DbException.get(DbException.java:199) + at org.h2.message.DbException.get(DbException.java:188) + at org.h2.engine.Engine.openSession(Engine.java:154) + at org.h2.engine.Engine.openSession(Engine.java:222) + at org.h2.engine.Engine.createSession(Engine.java:201) + at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:343) + at org.h2.jdbc.JdbcConnection.(JdbcConnection.java:125) + at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:288) + at org.h2.server.web.WebServer.getConnection(WebServer.java:811) + at org.h2.server.web.WebApp.login(WebApp.java:1039) + at org.h2.server.web.WebApp.process(WebApp.java:227) + at org.h2.server.web.WebApp.processRequest(WebApp.java:177) + at org.h2.server.web.JakartaWebServlet.doGet(JakartaWebServlet.java:129) + at org.h2.server.web.JakartaWebServlet.doPost(JakartaWebServlet.java:166) + at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590) + at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108) + at org.springframework.security.web.FilterChainProxy.lambda$doFilterInternal$3(FilterChainProxy.java:231) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:365) + at org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:100) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:126) + at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:120) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:100) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:110) + at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:101) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:179) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:227) + at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:221) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:107) + at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:93) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:91) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90) + at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.context.SecurityContextHolderFilter.doFilter(SecurityContextHolderFilter.java:82) + at org.springframework.security.web.context.SecurityContextHolderFilter.doFilter(SecurityContextHolderFilter.java:69) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:62) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.session.DisableEncodeUrlFilter.doFilterInternal(DisableEncodeUrlFilter.java:42) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:374) + at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:233) + at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:191) + at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:113) + at org.springframework.web.servlet.handler.HandlerMappingIntrospector.lambda$createCacheFilter$3(HandlerMappingIntrospector.java:195) + at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:113) + at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:74) + at org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy.doFilter(WebMvcSecurityConfiguration.java:230) + at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:352) + at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:268) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150) + at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) + at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) + at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) + at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) + at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) + at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) + at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) + at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) + at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) + at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) + at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736) + at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) + at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) + at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) + at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63) + at java.base/java.lang.Thread.run(Thread.java:1623) +Caused by: org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Неверное имя пользователя или пароль +Wrong user name or password [28000-224] + at org.h2.message.DbException.getJdbcSQLException(DbException.java:522) + at org.h2.message.DbException.getJdbcSQLException(DbException.java:489) + ... 100 more diff --git a/src/main/java/com/example/demo/games/api/GameController.java b/src/main/java/com/example/demo/games/api/GameController.java index e72cc73..f7045ed 100644 --- a/src/main/java/com/example/demo/games/api/GameController.java +++ b/src/main/java/com/example/demo/games/api/GameController.java @@ -6,21 +6,24 @@ import java.util.Map; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.DeleteMapping; +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.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.demo.core.api.PageAttributesMapper; import com.example.demo.core.configuration.Constants; import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; +import com.example.demo.genres.api.GenreDto; import com.example.demo.genres.model.GenreEntity; import com.example.demo.genres.service.GenreService; +import com.example.demo.types.api.TypeDto; +import com.example.demo.types.model.TypeEntity; import com.example.demo.types.service.TypeService; import jakarta.validation.Valid; @@ -60,6 +63,14 @@ public class GameController { return dto; } + private TypeDto toTypeDto(TypeEntity entity) { + return modelMapper.map(entity, TypeDto.class); + } + + private GenreDto toGenreDto(GenreEntity entity) { + return modelMapper.map(entity, GenreDto.class); + } + private GameEntity toEntity(GameDto dto) { // return modelMapper.map(dto, GameEntity.class); final GameEntity entity = modelMapper.map(dto, GameEntity.class); @@ -83,23 +94,75 @@ public class GameController { return GAME_VIEW; } - @GetMapping("/{id}") - public GameDto get(@PathVariable(name = "id") Long id) { - return toDto(gameService.get(id)); + @GetMapping("/edit/") + public String create( + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + model.addAttribute(GAME_ATTRIBUTE, new GameDto()); + model.addAttribute("types", typeService.getAll().stream().map(this::toTypeDto).toList()); + model.addAttribute("genresCheck", genreService.getAll().stream().map(this::toGenreDto).toList()); + model.addAttribute(PAGE_ATTRIBUTE, page); + return GAME_EDIT_VIEW; } - @PostMapping - public GameDto create(@RequestBody @Valid GameDto dto) { - return toDto(gameService.create(toEntity(dto))); + @PostMapping("/edit/") + public String create( + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = GAME_ATTRIBUTE) @Valid GameDto game, + BindingResult bindingResult, + Model model, + RedirectAttributes redirectAttributes) { + if (bindingResult.hasErrors()) { + model.addAttribute(PAGE_ATTRIBUTE, page); + return GAME_EDIT_VIEW; + } + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + gameService.create(toEntity(game)); + return Constants.REDIRECT_VIEW + URL; } - @PutMapping("/{id}") - public GameDto update(@PathVariable(name = "id") Long id, @RequestBody GameDto dto) { - return toDto(gameService.update(id, toEntity(dto))); + @GetMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + if (id <= 0) { + throw new IllegalArgumentException(); + } + model.addAttribute(GAME_ATTRIBUTE, toDto(gameService.get(id))); + model.addAttribute(PAGE_ATTRIBUTE, page); + model.addAttribute("types", typeService.getAll().stream().map(this::toTypeDto).toList()); + model.addAttribute("genresCheck", genreService.getAll().stream().map(this::toGenreDto).toList()); + return GAME_EDIT_VIEW; } - @DeleteMapping("/{id}") - public GameDto delete(@PathVariable(name = "id") Long id) { - return toDto(gameService.delete(id)); + @PostMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + @ModelAttribute(name = GAME_ATTRIBUTE) @Valid GameDto game, + BindingResult bindingResult, + Model model, + RedirectAttributes redirectAttributes) { + if (bindingResult.hasErrors()) { + model.addAttribute(PAGE_ATTRIBUTE, page); + return GAME_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + gameService.update(id, toEntity(game)); + return Constants.REDIRECT_VIEW + URL; + } + + @PostMapping("/delete/{id}") + public String delete( + @PathVariable(name = "id") Long id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + RedirectAttributes redirectAttributes) { + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + gameService.delete(id); + return Constants.REDIRECT_VIEW + URL; } } diff --git a/src/main/java/com/example/demo/games/api/GameDto.java b/src/main/java/com/example/demo/games/api/GameDto.java index 1a6e39f..a71abb3 100644 --- a/src/main/java/com/example/demo/games/api/GameDto.java +++ b/src/main/java/com/example/demo/games/api/GameDto.java @@ -15,7 +15,7 @@ public class GameDto { @NotNull private final List genres = new ArrayList<>(); @NotNull - @Min(1) + @Min(100) private Double price; @NotBlank private String name; diff --git a/src/main/java/com/example/demo/games/model/GameEntity.java b/src/main/java/com/example/demo/games/model/GameEntity.java index ff75593..a3573b0 100644 --- a/src/main/java/com/example/demo/games/model/GameEntity.java +++ b/src/main/java/com/example/demo/games/model/GameEntity.java @@ -12,12 +12,12 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToMany; -import jakarta.persistence.OneToOne; +import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; @Entity @Table(name = "games") -public class GameEntity extends BaseEntity{ +public class GameEntity extends BaseEntity { @Column(nullable = false) private String name; @Column(nullable = false) @@ -27,14 +27,14 @@ public class GameEntity extends BaseEntity{ @ManyToMany() @Column(unique = false) private final List genres = new ArrayList<>(); - @OneToOne + @ManyToOne @JoinColumn(name = "typeId", nullable = false, unique = false) private TypeEntity type; - public GameEntity(){ + public GameEntity() { } - public GameEntity(TypeEntity type, String name, Double price, String description, List genres){ + public GameEntity(TypeEntity type, String name, Double price, String description, List genres) { this.type = type; this.name = name; this.price = price; @@ -43,63 +43,67 @@ public class GameEntity extends BaseEntity{ this.genres.addAll(genres); } - public TypeEntity getType(){ + public TypeEntity getType() { return type; } - public void setType(TypeEntity type){ + public void setType(TypeEntity type) { this.type = type; } - public Double getPrice(){ + public Double getPrice() { return price; } - public void setPrice(Double price){ + public void setPrice(Double price) { this.price = price; } - public String getName(){ + public String getName() { return name; } - public void setName(String name){ + public void setName(String name) { this.name = name; } - public String getDescription(){ + public String getDescription() { return description; } - public void setDescription(String description){ + public void setDescription(String description) { this.description = description; } - public List getGenres(){ + public List getGenres() { return genres; } - public void setGenres(GenreEntity genre){ + public void setGenres(GenreEntity genre) { this.genres.add(genre); } + public void delGenres() { + this.genres.clear(); + } + @Override public int hashCode() { return Objects.hash(id, type, price, genres, description, name); } @Override - public boolean equals(Object obj){ - if(this == obj) + public boolean equals(Object obj) { + if (this == obj) return true; - if(obj == null || getClass() != obj.getClass()) + if (obj == null || getClass() != obj.getClass()) return false; final GameEntity other = (GameEntity) obj; return Objects.equals(other.getId(), id) - && Objects.equals(other.getType(), type) - && Objects.equals(other.getPrice(), price) - && Objects.equals(other.getGenres(), genres) - && Objects.equals(other.getDescription(), description) - && Objects.equals(other.getName(), name); + && Objects.equals(other.getType(), type) + && Objects.equals(other.getPrice(), price) + && Objects.equals(other.getGenres(), genres) + && Objects.equals(other.getDescription(), description) + && Objects.equals(other.getName(), name); } } diff --git a/src/main/java/com/example/demo/games/service/GameService.java b/src/main/java/com/example/demo/games/service/GameService.java index 3bafb07..cf79b52 100644 --- a/src/main/java/com/example/demo/games/service/GameService.java +++ b/src/main/java/com/example/demo/games/service/GameService.java @@ -86,8 +86,11 @@ public class GameService { existEntity.setDescription(entity.getDescription()); existEntity.setType(entity.getType()); var genres = entity.getGenres(); - for (var genre : genres) { - existEntity.setGenres(genre); + if (genres.size() != 0) { + existEntity.delGenres(); + for (var genre : genres) { + existEntity.setGenres(genre); + } } return repository.save(existEntity); } diff --git a/src/main/java/com/example/demo/genres/api/GenreDto.java b/src/main/java/com/example/demo/genres/api/GenreDto.java index 5f3c6e0..343b96c 100644 --- a/src/main/java/com/example/demo/genres/api/GenreDto.java +++ b/src/main/java/com/example/demo/genres/api/GenreDto.java @@ -1,30 +1,27 @@ package com.example.demo.genres.api; -import com.fasterxml.jackson.annotation.JsonProperty; - import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; public class GenreDto { - @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotBlank @Size(min = 1, max = 50) private String name; - public Long getId(){ + public Long getId() { return id; } - - public void setId(Long id){ + + public void setId(Long id) { this.id = id; } - public String getName(){ + public String getName() { return name; } - - public void setName(String name){ + + public void setName(String name) { this.name = name; } } diff --git a/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java index 9015a75..fa7cbab 100644 --- a/src/main/java/com/example/demo/orders/repository/OrderRepository.java +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -15,6 +15,7 @@ public interface OrderRepository extends CrudRepository, PagingAndSortingRepository { Optional findOneByUserIdAndId(long userId, long id); + @Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1") List findByUserId(long userId); @Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1") diff --git a/src/main/java/com/example/demo/orders/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java index 780152f..ea6f109 100644 --- a/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -1,7 +1,6 @@ package com.example.demo.orders.service; import java.util.List; -import java.util.stream.StreamSupport; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; diff --git a/src/main/java/com/example/demo/users/api/UserCartController.java b/src/main/java/com/example/demo/users/api/UserCartController.java index 5499f2a..572fb10 100644 --- a/src/main/java/com/example/demo/users/api/UserCartController.java +++ b/src/main/java/com/example/demo/users/api/UserCartController.java @@ -2,9 +2,7 @@ package com.example.demo.users.api; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.function.Function; import java.util.stream.Collectors; import org.modelmapper.ModelMapper; @@ -43,7 +41,6 @@ public class UserCartController { private final GameService gameService; private final OrderService orderService; private final SessionCart cart; - private final ModelMapper modelMapper; public UserCartController( GameService gameService, @@ -53,7 +50,6 @@ public class UserCartController { this.gameService = gameService; this.orderService = orderService; this.cart = cart; - this.modelMapper = modelMapper; } private GameDto toGameDto(GameEntity entity) { @@ -68,14 +64,14 @@ public class UserCartController { } // private List toGameEntities(Collection dtos){ - // final GameEntity entity = modelMapper.map(dto, GameEntity.class); - // entity.setType(typeService.get(dto.getTypeId())); - // var genres = dto.getGenres(); - // List genresList = genreService.getAllById(genres); - // for (var genre : genresList) { - // entity.setGenres(genre); - // } - // return entity; + // final GameEntity entity = modelMapper.map(dto, GameEntity.class); + // entity.setType(typeService.get(dto.getTypeId())); + // var genres = dto.getGenres(); + // List genresList = genreService.getAllById(genres); + // for (var genre : genresList) { + // entity.setGenres(genre); + // } + // return entity; // } private OrderEntity toOrderEntities(Collection dtos) { diff --git a/src/main/java/com/example/demo/users/api/UserProfileController.java b/src/main/java/com/example/demo/users/api/UserProfileController.java index f480f40..82b5db1 100644 --- a/src/main/java/com/example/demo/users/api/UserProfileController.java +++ b/src/main/java/com/example/demo/users/api/UserProfileController.java @@ -5,7 +5,10 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.demo.core.api.PageAttributesMapper; import com.example.demo.core.configuration.Constants; @@ -24,13 +27,9 @@ public class UserProfileController { private static final String PROFILE_VIEW = "profile"; private static final String PAGE_ATTRIBUTE = "page"; - private static final String TYPEID_ATTRIBUTE = "gameId"; - private static final String PROFILE_ATTRIBUTE = "profile"; private final OrderService orderService; private final GameService gameService; - private final UserService userService; - private final ModelMapper modelMapper; public UserProfileController( OrderService orderService, @@ -39,8 +38,6 @@ public class UserProfileController { ModelMapper modelMapper) { this.orderService = orderService; this.gameService = gameService; - this.userService = userService; - this.modelMapper = modelMapper; } private OrderDto toDto(OrderEntity entity) { @@ -64,12 +61,10 @@ public class UserProfileController { @GetMapping public String getProfile( @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, - @RequestParam(name = TYPEID_ATTRIBUTE, defaultValue = "0") int typeId, Model model, @AuthenticationPrincipal UserPrincipal principal) { final long userId = principal.getId(); model.addAttribute(PAGE_ATTRIBUTE, page); - model.addAttribute(TYPEID_ATTRIBUTE, typeId); model.addAllAttributes(PageAttributesMapper.toAttributes( orderService.getAll(userId, page, Constants.DEFAULT_PAGE_SIZE), this::toDto)); @@ -79,4 +74,15 @@ public class UserProfileController { .toList()); return PROFILE_VIEW; } + + @PostMapping("/delete/{id}") + public String deleteOrder( + @PathVariable(name = "id") Long id, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + RedirectAttributes redirectAttributes, + @AuthenticationPrincipal UserPrincipal principal) { + redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); + orderService.delete(principal.getId(), id); + return Constants.REDIRECT_VIEW + "/"; + } } diff --git a/src/main/resources/templates/cart.html b/src/main/resources/templates/cart.html index a19c372..c595b07 100644 --- a/src/main/resources/templates/cart.html +++ b/src/main/resources/templates/cart.html @@ -17,12 +17,15 @@ -
+
-
- [[${cartItem.gameName}]] +
+ Название: [[${cartItem.gameName}]]
+
+ Цена: [[${cartItem.Price}]] +
diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 2be9f25..0e7b41c 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -47,8 +47,6 @@ Консоль H2 - Ошибка 1 - Ошибка 2