Skip to content

Commit

Permalink
Merge pull request #30 from denomelchenko/feature/admin-panel-item
Browse files Browse the repository at this point in the history
Feature/admin panel item
  • Loading branch information
denomelchenko authored Jun 21, 2024
2 parents fea5dd3 + 4298cd1 commit 479471b
Show file tree
Hide file tree
Showing 31 changed files with 398 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lalabrand.ecommerce.exception;

public class AccessDeniedException extends RuntimeException {
public AccessDeniedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import graphql.schema.DataFetchingEnvironment;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
import org.apache.coyote.BadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -35,16 +32,17 @@ protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironmen
private ErrorType determineErrorType(Throwable ex) {
if (ex instanceof IllegalArgumentException
|| ex instanceof ConstraintViolationException
|| ex instanceof TokenExpiredException
|| ex instanceof BadRequestException) {
|| ex instanceof TokenExpiredException) {
return ErrorType.BAD_REQUEST;
} else if (ex instanceof BadCredentialsException) {
return ErrorType.UNAUTHORIZED;
} else if (ex instanceof AccessDeniedException) {
} else if (ex instanceof AccessDeniedException
|| ex instanceof org.springframework.security.access.AccessDeniedException) {
return ErrorType.FORBIDDEN;
} else if (ex instanceof EntityNotFoundException
|| ex instanceof UsernameNotFoundException
|| ex instanceof UserAlreadyExistException) {
|| ex instanceof UserAlreadyExistException
|| ex instanceof org.springframework.security.core.userdetails.UsernameNotFoundException) {
return ErrorType.NOT_FOUND;
} else {
return ErrorType.INTERNAL_ERROR;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lalabrand.ecommerce.exception;

public class UsernameNotFoundException extends RuntimeException {
public UsernameNotFoundException(String message) {
super(message);
}
}
18 changes: 8 additions & 10 deletions src/main/java/com/lalabrand/ecommerce/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.lalabrand.ecommerce.item.look.Look;
import com.lalabrand.ecommerce.order.ordered_item.OrderedItem;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand All @@ -22,6 +19,7 @@
@Getter
@Setter
@Entity
@Builder
@Table(name = "item")
public class Item {
@Id
Expand All @@ -46,24 +44,24 @@ public class Item {

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.SET_NULL)
@JoinColumn(name = "category_id", nullable = false)
@JoinColumn(name = "category_id", nullable = false, insertable = false, updatable = false)
private Category category;

@Column(name = "category_id", nullable = false)
private String categoryId;

@Column(name = "available_count", nullable = false)
private Integer availableCount;

@Column(name = "sale_price")
private BigDecimal salePrice;

@Column(name = "image", nullable = false)
private String image;

@Column(name = "sold_count", nullable = false)
private Integer soldCount;

@Column(name = "created_at", nullable = false)

private Instant createdAt;

@OneToMany(mappedBy = "item")
private Set<ItemComment> itemComments;

Expand All @@ -86,4 +84,4 @@ public void prePersist() {
createdAt = Instant.now();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.lalabrand.ecommerce.utils.PaginationRequest;
import jakarta.validation.constraints.NotBlank;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

import java.util.List;
Expand Down Expand Up @@ -44,4 +46,10 @@ public List<ItemDTO> findItemsByTitle(@Argument @NotBlank String title,
return itemService.findItemsByTitle(title, language,
paginationRequest.toPageRequest());
}

@MutationMapping(name = "item")
@PreAuthorize("hasAuthority('ADMIN')")
public ItemDTO createItem(@Argument ItemInput itemInput) {
return itemService.save(itemInput);
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/lalabrand/ecommerce/item/ItemDTO.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.lalabrand.ecommerce.item;

import com.lalabrand.ecommerce.item.item_info.ItemInfoDTO;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -22,20 +21,20 @@
@Builder
public class ItemDTO implements Serializable {
String id;
@NotNull
@NotBlank
String title;
String shortDisc;
String longDisc;
BigDecimal rating;
BigDecimal price;
Integer availableCount;
BigDecimal salePrice;
String image;
Integer soldCount;
Set<ItemInfoDTO> itemInfos;

public static ItemDTO fromEntity(Item item) {
if (item.getItemInfos() == null) {
item.setItemInfos(new HashSet<>());
}
return ItemDTO.builder()
.id(item.getId())
.title(item.getTitle())
Expand All @@ -45,7 +44,6 @@ public static ItemDTO fromEntity(Item item) {
.price(item.getPrice())
.availableCount(item.getAvailableCount())
.salePrice(item.getSalePrice())
.image(item.getImage())
.soldCount(item.getSoldCount())
.itemInfos(item.getItemInfos().stream().map(ItemInfoDTO::fromEntity).collect(Collectors.toSet()))
.build();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/lalabrand/ecommerce/item/ItemInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lalabrand.ecommerce.item;

import lombok.Getter;
import lombok.Setter;
import lombok.Value;

import java.math.BigDecimal;

@Getter
@Setter
@Value
public class ItemInput {
String title;
String shortDisc;
String longDisc;
BigDecimal rating;
BigDecimal price;
Integer availableCount;
BigDecimal salePrice;
Integer soldCount;
String categoryId;

public Item toEntity() {
return Item.builder()
.title(this.title)
.shortDisc(this.shortDisc)
.longDisc(this.longDisc)
.rating(this.rating)
.price(this.price)
.availableCount(this.availableCount)
.salePrice(this.salePrice)
.soldCount(this.soldCount)
.categoryId(categoryId)
.build();
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/lalabrand/ecommerce/item/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ private List<ItemDTO> convertToItemDtoList(List<Item> items) {
public ItemDTO findById(String id) {
Optional<Item> item = itemRepository.findById(id);
if (item.isEmpty()) {
throw new IllegalArgumentException("Item with this id does not exist");
throw new IllegalArgumentException("Item wit id: " + id + " does not exist");
}
return ItemDTO.fromEntity(item.get());
}

public ItemDTO save(ItemInput itemInput) {
return ItemDTO.fromEntity(itemRepository.save(itemInput.toEntity()));
}
}
23 changes: 15 additions & 8 deletions src/main/java/com/lalabrand/ecommerce/item/item_info/ItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import com.lalabrand.ecommerce.item.enums.ColorEnum;
import com.lalabrand.ecommerce.item.size.Size;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

import java.util.LinkedHashSet;
import java.util.Set;

@Getter
@Setter
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "item_info")
public class ItemInfo {
@Id
Expand All @@ -22,10 +26,6 @@ public class ItemInfo {
@JoinColumn(name = "item_id", nullable = false, insertable = false, updatable = false)
private Item item;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "size_id", nullable = false)
private Size size;

@Column(name = "item_id", nullable = false)
private String itemId;

Expand All @@ -36,7 +36,14 @@ public class ItemInfo {
@Column(name = "image")
private String image;

@NotNull
@ManyToMany
@JoinTable(
name = "items_sizes",
joinColumns = @JoinColumn(name = "item_info_id"),
inverseJoinColumns = @JoinColumn(name = "size_id")
)
private Set<Size> sizes = new LinkedHashSet<>();

@Column(name = "is_color_available", nullable = false)
private Boolean isColorAvailable = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.lalabrand.ecommerce.item.item_info;

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

import java.util.Set;

@Controller
public class ItemInfoController {
private final ItemInfoService itemInfoService;

public ItemInfoController(ItemInfoService itemInfoService) {
this.itemInfoService = itemInfoService;
}

@MutationMapping(name = "sizeToItemInfo")
@PreAuthorize("hasAuthority('ADMIN')")
public ItemInfoDTO addSizeToItemInfo(@Argument String itemInfoId, @Argument String sizeId) {
return itemInfoService.addSizeToItemInfo(itemInfoId, sizeId);
}

@MutationMapping(name = "sizesToItemInfo")
@PreAuthorize("hasAuthority('ADMIN')")
public ItemInfoDTO addSizesToItemInfo(@Argument String itemInfoId, @Argument Set<String> sizeIds) {
return itemInfoService.addSizesToItemInfo(itemInfoId, sizeIds);
}

@MutationMapping(name = "itemInfo")
@PreAuthorize("hasAuthority('ADMIN')")
public ItemInfoDTO createItemInfo(@Argument ItemInfoInput itemInfoInput) {
return itemInfoService.save(itemInfoInput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
import com.lalabrand.ecommerce.item.enums.ColorEnum;
import com.lalabrand.ecommerce.item.size.SizeDTO;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;

import java.io.Serializable;
import java.util.Set;
import java.util.stream.Collectors;

/**
* DTO for {@link ItemInfo}
*/
@Value
@Getter
@Setter
@Builder
public class ItemInfoDTO implements Serializable {
String id;
String image;
ColorEnum color;
SizeDTO size;
Set<SizeDTO> sizes;
String itemId;
Boolean isColorAvailable;

public static ItemInfoDTO fromEntity(ItemInfo itemInfo) {
return ItemInfoDTO.builder()
.id(itemInfo.getId())
.color(itemInfo.getColor())
.itemId(itemInfo.getItemId())
.image(itemInfo.getImage())
.size(SizeDTO.fromEntity(itemInfo.getSize()))
.sizes(itemInfo.getSizes().stream().map(SizeDTO::fromEntity).collect(Collectors.toSet()))
.isColorAvailable(itemInfo.getIsColorAvailable())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lalabrand.ecommerce.item.item_info;

import com.lalabrand.ecommerce.item.enums.ColorEnum;
import com.lalabrand.ecommerce.utils.Id;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;

@Value
@Getter
@Setter
public class ItemInfoInput {
String image;
ColorEnum color;
Boolean isColorAvailable;
@Id
String itemId;

public ItemInfo toEntity() {
return ItemInfo.builder()
.color(this.color)
.image(this.image)
.isColorAvailable(this.isColorAvailable)
.itemId(this.itemId)
.build();
}
}
Loading

0 comments on commit 479471b

Please sign in to comment.