Search in sources :

Example 1 with EventCreationRequest

use of alfio.model.api.v1.admin.EventCreationRequest in project alf.io by alfio-event.

the class EventApiV1Controller method insertEvent.

private Optional<Event> insertEvent(EventCreationRequest request, Principal user, String imageRef) {
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    EventModification em = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
    eventManager.createEvent(em, user.getName());
    Optional<Event> event = eventManager.getOptionalByName(em.getShortName(), user.getName());
    event.ifPresent(e -> {
        Optional.ofNullable(request.getTickets().getPromoCodes()).ifPresent(promoCodes -> promoCodes.forEach(// TODO add ref to categories
        pc -> eventManager.addPromoCode(pc.getName(), e.getId(), organization.getId(), ZonedDateTime.of(pc.getValidFrom(), e.getZoneId()), ZonedDateTime.of(pc.getValidTo(), e.getZoneId()), pc.getDiscount(), pc.getDiscountType(), Collections.emptyList(), null, null, null, PromoCodeDiscount.CodeType.DISCOUNT, null)));
        // link categories to groups, if any
        request.getTickets().getCategories().stream().filter(cr -> cr.getGroupLink() != null && cr.getGroupLink().getGroupId() != null).map(cr -> Pair.of(cr, groupManager.findById(cr.getGroupLink().getGroupId(), organization.getId()))).forEach(link -> {
            if (link.getRight().isPresent()) {
                Group group = link.getRight().get();
                EventCreationRequest.CategoryRequest categoryRequest = link.getLeft();
                findCategoryByName(e, categoryRequest.getName()).ifPresent(category -> {
                    EventCreationRequest.GroupLinkRequest groupLinkRequest = categoryRequest.getGroupLink();
                    LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), e.getId(), category.getId(), groupLinkRequest.getType(), groupLinkRequest.getMatchType(), groupLinkRequest.getMaxAllocation());
                    groupManager.createLink(group.getId(), e.getId(), modification);
                });
            }
        });
        if (!CollectionUtils.isEmpty(request.getExtensionSettings())) {
            request.getExtensionSettings().stream().collect(Collectors.groupingBy(EventCreationRequest.ExtensionSetting::getExtensionId)).forEach((id, settings) -> {
                List<ExtensionSupport.ExtensionMetadataIdAndName> metadata = extensionService.getSingle(organization, e, id).map(es -> extensionRepository.findAllParametersForExtension(es.getId())).orElseGet(Collections::emptyList);
                List<ExtensionMetadataValue> values = settings.stream().map(es -> Pair.of(es, metadata.stream().filter(mm -> mm.getName().equals(es.getKey())).findFirst())).filter(pair -> {
                    if (pair.getRight().isEmpty()) {
                        log.warn("ignoring non-existent extension setting key {}", pair.getLeft().getKey());
                    }
                    return pair.getRight().isPresent();
                }).map(pair -> new ExtensionMetadataValue(pair.getRight().get().getId(), pair.getLeft().getValue())).collect(Collectors.toList());
                extensionService.bulkUpdateEventSettings(organization, e, values);
            });
        }
    });
    return event;
}
Also used : ValidationResult(alfio.model.result.ValidationResult) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) alfio.manager(alfio.manager) Group(alfio.model.group.Group) Errors(org.springframework.validation.Errors) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ZonedDateTime(java.time.ZonedDateTime) ConfigurationManager(alfio.manager.system.ConfigurationManager) ExtensionRepository(alfio.repository.ExtensionRepository) EventModification(alfio.model.modification.EventModification) AtomicReference(java.util.concurrent.atomic.AtomicReference) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) CollectionUtils(org.apache.commons.collections.CollectionUtils) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Collectors(java.util.stream.Collectors) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) Result(alfio.model.result.Result) List(java.util.List) ExtensionService(alfio.extension.ExtensionService) Principal(java.security.Principal) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Group(alfio.model.group.Group) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) EventModification(alfio.model.modification.EventModification) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) Collections(java.util.Collections)

Example 2 with EventCreationRequest

use of alfio.model.api.v1.admin.EventCreationRequest in project alf.io by alfio-event.

the class EventApiV1Controller method updateEvent.

private Optional<Event> updateEvent(String slug, EventCreationRequest request, Principal user, String imageRef) {
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    EventWithAdditionalInfo original = eventStatisticsManager.getEventWithAdditionalInfo(slug, user.getName());
    Event event = original.getEvent();
    EventModification em = request.toEventModificationUpdate(original, organization, imageRef);
    eventManager.updateEventHeader(event, em, user.getName());
    eventManager.updateEventPrices(event, em, user.getName());
    if (em.getTicketCategories() != null && !em.getTicketCategories().isEmpty()) {
        em.getTicketCategories().forEach(c -> findCategoryByName(event, c.getName()).ifPresent(originalCategory -> eventManager.updateCategory(originalCategory.getId(), event.getId(), c, user.getName())));
    }
    return eventManager.getOptionalByName(slug, user.getName());
}
Also used : ValidationResult(alfio.model.result.ValidationResult) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) alfio.manager(alfio.manager) Group(alfio.model.group.Group) Errors(org.springframework.validation.Errors) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ZonedDateTime(java.time.ZonedDateTime) ConfigurationManager(alfio.manager.system.ConfigurationManager) ExtensionRepository(alfio.repository.ExtensionRepository) EventModification(alfio.model.modification.EventModification) AtomicReference(java.util.concurrent.atomic.AtomicReference) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) CollectionUtils(org.apache.commons.collections.CollectionUtils) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Collectors(java.util.stream.Collectors) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) Result(alfio.model.result.Result) List(java.util.List) ExtensionService(alfio.extension.ExtensionService) Principal(java.security.Principal) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Organization(alfio.model.user.Organization) EventModification(alfio.model.modification.EventModification) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent)

Example 3 with EventCreationRequest

use of alfio.model.api.v1.admin.EventCreationRequest in project alf.io by alfio-event.

the class EventApiV1IntegrationTest method createTest.

@Test
public void createTest() {
    EventCreationRequest eventCreationRequest = creationRequest();
    String shortName = controller.create(eventCreationRequest, mockPrincipal).getBody();
    Event event = eventManager.getSingleEvent(shortName, username);
    List<TicketCategory> tickets = ticketCategoryRepository.findAllTicketCategories(event.getId());
    assertEquals(eventCreationRequest.getTitle(), event.getDisplayName());
    assertEquals(eventCreationRequest.getSlug(), event.getShortName());
    assertEquals(eventCreationRequest.getTickets().getCurrency(), event.getCurrency());
    assertEquals(eventCreationRequest.getWebsiteUrl(), event.getWebsiteUrl());
    assertEquals(eventCreationRequest.getTickets().getPaymentMethods(), event.getAllowedPaymentProxies());
    Assertions.assertTrue(event.getFileBlobIdIsPresent());
    assertEquals(eventCreationRequest.getTickets().getCategories().size(), tickets.size());
    tickets.forEach((t) -> {
        List<EventCreationRequest.CategoryRequest> requestCategories = eventCreationRequest.getTickets().getCategories().stream().filter((rt) -> rt.getName().equals(t.getName())).collect(Collectors.toList());
        assertEquals(1, requestCategories.size());
        requestCategories.forEach((rtc) -> {
            assertEquals(t.getMaxTickets(), rtc.getMaxTickets().intValue());
            assertEquals(0, t.getPrice().compareTo(rtc.getPrice()));
        });
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) TicketCategoryRepository(alfio.repository.TicketCategoryRepository) PaymentProxy(alfio.model.transaction.PaymentProxy) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) BigDecimal(java.math.BigDecimal) BeforeAll(org.junit.jupiter.api.BeforeAll) OrganizationModification(alfio.model.modification.OrganizationModification) ControllerConfiguration(alfio.controller.api.ControllerConfiguration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventApiV1Controller(alfio.controller.api.v1.admin.EventApiV1Controller) BaseIntegrationTest(alfio.util.BaseIntegrationTest) OrganizationRepository(alfio.repository.user.OrganizationRepository) Organization(alfio.model.user.Organization) User(alfio.model.user.User) UUID(java.util.UUID) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) IntegrationTestUtil(alfio.test.util.IntegrationTestUtil) Collectors(java.util.stream.Collectors) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) TicketCategory(alfio.model.TicketCategory) Role(alfio.model.user.Role) List(java.util.List) Initializer(alfio.config.Initializer) EventManager(alfio.manager.EventManager) Principal(java.security.Principal) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) UserManager(alfio.manager.user.UserManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assertions(org.junit.jupiter.api.Assertions) DataSourceConfiguration(alfio.config.DataSourceConfiguration) Event(alfio.model.Event) TestConfiguration(alfio.TestConfiguration) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Event(alfio.model.Event) TicketCategory(alfio.model.TicketCategory) BaseIntegrationTest(alfio.util.BaseIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with EventCreationRequest

use of alfio.model.api.v1.admin.EventCreationRequest in project alf.io by alfio-event.

the class EventApiV1IntegrationTest method updateTest.

@Test
public void updateTest() {
    controller.create(creationRequest(), mockPrincipal);
    String newTitle = "new title";
    EventCreationRequest updateRequest = new EventCreationRequest(newTitle, null, null, null, null, null, null, null, null, null, null, null, new EventCreationRequest.TicketRequest(null, 10, null, null, null, null, null, null), null, null);
    controller.update(shortName, updateRequest, mockPrincipal);
    Event event = eventManager.getSingleEvent(shortName, username);
    assertEquals(newTitle, event.getDisplayName());
}
Also used : EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Event(alfio.model.Event) BaseIntegrationTest(alfio.util.BaseIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

EventCreationRequest (alfio.model.api.v1.admin.EventCreationRequest)4 UserManager (alfio.manager.user.UserManager)3 Organization (alfio.model.user.Organization)3 Principal (java.security.Principal)3 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Transactional (org.springframework.transaction.annotation.Transactional)3 EventApiController.validateEvent (alfio.controller.api.admin.EventApiController.validateEvent)2 ExtensionService (alfio.extension.ExtensionService)2 alfio.manager (alfio.manager)2 ConfigurationLevel (alfio.manager.system.ConfigurationLevel)2 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 alfio.model (alfio.model)2 Event (alfio.model.Event)2 ExtensionMetadataValue (alfio.model.ExtensionSupport.ExtensionMetadataValue)2 Group (alfio.model.group.Group)2 EventModification (alfio.model.modification.EventModification)2 LinkedGroupModification (alfio.model.modification.LinkedGroupModification)2 ErrorCode (alfio.model.result.ErrorCode)2