Search in sources :

Example 16 with Catalog

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

the class Catalogs method delete.

@DELETE
@Transactional
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ ADMIN, STORE_ADMIN })
@Path("/{catalogId}")
public void delete(@Context SecurityContext securityContext, @PathParam("catalogId") Long catalogId) {
    Catalog loadedCatalog = entityManager.find(Catalog.class, catalogId);
    checkNotNull(loadedCatalog);
    if (isAdminUser(securityContext) || isOwner(securityContext, loadedCatalog.getOwner()))
        entityManager.remove(loadedCatalog);
    else
        throw new WebApplicationException(Response.Status.FORBIDDEN);
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Catalog(org.rembx.jeeshop.catalog.model.Catalog) RolesAllowed(javax.annotation.security.RolesAllowed) Transactional(javax.transaction.Transactional)

Example 17 with Catalog

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

the class Stores method modify.

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

Example 18 with Catalog

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

the class Stores method findCatalogs.

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

Example 19 with Catalog

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

the class CatalogsCT method create_shouldSetupOwner_for_store_admin.

@Test
public void create_shouldSetupOwner_for_store_admin() {
    tester.setStoreAdminUser();
    Catalog catalog = new Catalog("Catalog");
    Catalog actualCatalog = tester.test_create(catalog);
    assertThat(actualCatalog).isNotNull();
    assertThat(actualCatalog.getOwner()).isEqualTo(TestCatalog.OWNER);
}
Also used : Catalog(org.rembx.jeeshop.catalog.model.Catalog) TestCatalog(org.rembx.jeeshop.catalog.test.TestCatalog) Test(org.junit.jupiter.api.Test)

Example 20 with Catalog

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

the class CatalogsCT method create_shouldThrowBadRequest_whenOwnerIsNull_for_admin.

@Test
public void create_shouldThrowBadRequest_whenOwnerIsNull_for_admin() {
    tester.setAdminUser();
    Catalog catalog = new Catalog("Catalog");
    try {
        tester.test_create(catalog);
        fail("should have thrown an exception");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Catalog(org.rembx.jeeshop.catalog.model.Catalog) TestCatalog(org.rembx.jeeshop.catalog.test.TestCatalog) Test(org.junit.jupiter.api.Test)

Aggregations

Catalog (org.rembx.jeeshop.catalog.model.Catalog)22 Test (org.junit.jupiter.api.Test)10 TestCatalog (org.rembx.jeeshop.catalog.test.TestCatalog)10 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)9 RolesAllowed (javax.annotation.security.RolesAllowed)7 ArrayList (java.util.ArrayList)6 PermitAll (javax.annotation.security.PermitAll)4 Category (org.rembx.jeeshop.catalog.model.Category)4 Transactional (javax.transaction.Transactional)3 Store (org.rembx.jeeshop.catalog.model.Store)2 Test (org.junit.Test)1 Presentation (org.rembx.jeeshop.catalog.model.Presentation)1