Search in sources :

Example 1 with Category

use of org.rembx.jeeshop.catalog.model.Category in project jeeshop by remibantos.

the class Catalogs method modify.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed(ADMIN)
public Catalog modify(Catalog catalog) {
    Catalog originalCatalog = entityManager.find(Catalog.class, catalog.getId());
    checkNotNull(originalCatalog);
    if (catalog.getRootCategoriesIds() != null) {
        List<Category> newCategories = new ArrayList<>();
        catalog.getRootCategoriesIds().forEach(categoryId -> newCategories.add(entityManager.find(Category.class, categoryId)));
        catalog.setRootCategories(newCategories);
    } else {
        catalog.setRootCategories(originalCatalog.getRootCategories());
    }
    catalog.setPresentationByLocale(originalCatalog.getPresentationByLocale());
    return entityManager.merge(catalog);
}
Also used : Category(org.rembx.jeeshop.catalog.model.Category) ArrayList(java.util.ArrayList) Catalog(org.rembx.jeeshop.catalog.model.Catalog) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 2 with Category

use of org.rembx.jeeshop.catalog.model.Category in project jeeshop by remibantos.

the class Catalogs method findCategories.

@GET
@Path("/{catalogId}/categories")
@Produces(MediaType.APPLICATION_JSON)
@PermitAll
public List<Category> findCategories(@PathParam("catalogId") @NotNull Long catalogId, @QueryParam("locale") String locale) {
    Catalog catalog = entityManager.find(Catalog.class, catalogId);
    checkNotNull(catalog);
    List<Category> rootCategories = catalog.getRootCategories();
    if (rootCategories.isEmpty()) {
        return new ArrayList<>();
    }
    if (isAdminUser(sessionContext)) {
        return rootCategories;
    } else {
        return catalogItemFinder.findVisibleCatalogItems(category, rootCategories, locale);
    }
}
Also used : Category(org.rembx.jeeshop.catalog.model.Category) ArrayList(java.util.ArrayList) Catalog(org.rembx.jeeshop.catalog.model.Catalog) PermitAll(javax.annotation.security.PermitAll)

Example 3 with Category

use of org.rembx.jeeshop.catalog.model.Category in project jeeshop by remibantos.

the class Catalogs method findCategories.

@GET
@Path("/{catalogId}/categories")
@Produces(MediaType.APPLICATION_JSON)
@PermitAll
public List<Category> findCategories(@Context SecurityContext securityContext, @PathParam("catalogId") @NotNull Long catalogId, @QueryParam("locale") String locale) {
    Catalog catalog = entityManager.find(Catalog.class, catalogId);
    checkNotNull(catalog);
    List<Category> rootCategories = catalog.getRootCategories();
    if (rootCategories.isEmpty()) {
        return new ArrayList<>();
    }
    if (isAdminUser(securityContext) || isOwner(securityContext, catalog.getOwner())) {
        return rootCategories;
    } else {
        return catalogItemFinder.findVisibleCatalogItems(category, rootCategories, locale);
    }
}
Also used : Category(org.rembx.jeeshop.catalog.model.Category) ArrayList(java.util.ArrayList) Catalog(org.rembx.jeeshop.catalog.model.Catalog) PermitAll(javax.annotation.security.PermitAll)

Example 4 with Category

use of org.rembx.jeeshop.catalog.model.Category in project jeeshop by remibantos.

the class Catalogs method modify.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@RolesAllowed({ ADMIN, STORE_ADMIN })
public Catalog modify(@Context SecurityContext securityContext, Catalog catalogToModify) {
    Catalog originalCatalog = entityManager.find(Catalog.class, catalogToModify.getId());
    checkNotNull(originalCatalog);
    if (!isAdminUser(securityContext) && !isOwner(securityContext, originalCatalog.getOwner()))
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    if (catalogToModify.getRootCategoriesIds() != null) {
        List<Category> newCategories = new ArrayList<>();
        catalogToModify.getRootCategoriesIds().forEach(categoryId -> newCategories.add(entityManager.find(Category.class, categoryId)));
        catalogToModify.setRootCategories(newCategories);
    } else {
        catalogToModify.setRootCategories(originalCatalog.getRootCategories());
    }
    catalogToModify.setPresentationByLocale(originalCatalog.getPresentationByLocale());
    return entityManager.merge(catalogToModify);
}
Also used : Category(org.rembx.jeeshop.catalog.model.Category) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) ArrayList(java.util.ArrayList) Catalog(org.rembx.jeeshop.catalog.model.Catalog) RolesAllowed(javax.annotation.security.RolesAllowed) Transactional(javax.transaction.Transactional)

Example 5 with Category

use of org.rembx.jeeshop.catalog.model.Category in project jeeshop by remibantos.

the class CategoriesCT method modifyCategory_ShouldModifyCategoryAttributesAndPreserveChildProductsWhenNotProvided.

@Test
public void modifyCategory_ShouldModifyCategoryAttributesAndPreserveChildProductsWhenNotProvided() {
    tester.setAdminUser();
    Category category = new Category(tester.getFixtures().aCategoryWithProducts().getId(), "New name");
    ;
    tester.test_modify(category);
    assertThat(category.getName()).isEqualTo("New name");
    assertThat(category.getChildProducts()).isNotEmpty();
}
Also used : Category(org.rembx.jeeshop.catalog.model.Category) Test(org.junit.jupiter.api.Test)

Aggregations

Category (org.rembx.jeeshop.catalog.model.Category)17 Test (org.junit.jupiter.api.Test)12 ArrayList (java.util.ArrayList)4 Catalog (org.rembx.jeeshop.catalog.model.Catalog)4 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)4 Date (java.util.Date)2 PermitAll (javax.annotation.security.PermitAll)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Transactional (javax.transaction.Transactional)1 Test (org.junit.Test)1