/// <summary> /// Creates a model view with a material price information /// </summary> /// <param name="materialPriceTableEntry">MaterialPriceTableEntry with the material price</param> /// <returns>GetMaterialPriceModelView with the material price information model view</returns> public static GetMaterialPriceModelView fromMaterialEntity(MaterialPriceTableEntry materialPriceTableEntry, string currency, string area) { GetMaterialPriceModelView getMaterialPriceModelView = new GetMaterialPriceModelView(); getMaterialPriceModelView.materialId = materialPriceTableEntry.entity.Id; getMaterialPriceModelView.id = materialPriceTableEntry.Id; getMaterialPriceModelView.value = materialPriceTableEntry.price.value; getMaterialPriceModelView.currency = currency; getMaterialPriceModelView.area = area; getMaterialPriceModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(materialPriceTableEntry.timePeriod.startingDate); getMaterialPriceModelView.endingDate = LocalDateTimePattern.GeneralIso.Format(materialPriceTableEntry.timePeriod.endingDate); return(getMaterialPriceModelView); }
/// <summary> /// Transforms an AddMaterialPriceTableEntry into a MaterialPriceTableEntry and saves it to the database /// </summary> /// <param name="modelView">material price table entry to transform and persist</param> /// <returns>created instance or null in case the creation wasn't successfull</returns> public static GetMaterialPriceModelView create(AddPriceTableEntryModelView modelView, IHttpClientFactory clientFactory) { string defaultCurrency = CurrencyPerAreaConversionService.getBaseCurrency(); string defaultArea = CurrencyPerAreaConversionService.getBaseArea(); long materialId = modelView.entityId; Material material = PersistenceContext.repositories().createMaterialRepository().find(materialId); if (material == null) { throw new ResourceNotFoundException(MATERIAL_NOT_FOUND); } string startingDateAsString = modelView.priceTableEntry.startingDate; string endingDateAsString = modelView.priceTableEntry.endingDate; LocalDateTime startingDate; LocalDateTime endingDate; LocalDateTime currentTime = NodaTime.LocalDateTime.FromDateTime(SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc()); try { startingDate = LocalDateTimePattern.GeneralIso.Parse(startingDateAsString).GetValueOrThrow(); if (startingDate.CompareTo(currentTime) < 0) { throw new InvalidOperationException(PAST_DATE); } } catch (UnparsableValueException) { throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText); } TimePeriod timePeriod = null; if (endingDateAsString != null) { try { endingDate = LocalDateTimePattern.GeneralIso.Parse(endingDateAsString).GetValueOrThrow(); if (endingDate.CompareTo(currentTime) < 0) { throw new InvalidOperationException(PAST_DATE); } timePeriod = TimePeriod.valueOf(startingDate, endingDate); } catch (UnparsableValueException) { throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText); } } else { timePeriod = TimePeriod.valueOf(startingDate); } CurrenciesService.checkCurrencySupport(modelView.priceTableEntry.price.currency); AreasService.checkAreaSupport(modelView.priceTableEntry.price.area); Price price = null; try { if (defaultCurrency.Equals(modelView.priceTableEntry.price.currency) && defaultArea.Equals(modelView.priceTableEntry.price.area)) { price = Price.valueOf(modelView.priceTableEntry.price.value); } else { Task <double> convertedValueTask = new CurrencyPerAreaConversionService(clientFactory) .convertCurrencyPerAreaToDefaultCurrencyPerArea( modelView.priceTableEntry.price.currency, modelView.priceTableEntry.price.area, modelView.priceTableEntry.price.value); convertedValueTask.Wait(); double convertedValue = convertedValueTask.Result; price = Price.valueOf(convertedValue); } } catch (HttpRequestException) { price = Price.valueOf(modelView.priceTableEntry.price.value); } MaterialPriceTableEntry materialPriceTableEntry = new MaterialPriceTableEntry(material, price, timePeriod); MaterialPriceTableEntry savedMaterialPriceTableEntry = PersistenceContext.repositories().createMaterialPriceTableRepository().save(materialPriceTableEntry); if (savedMaterialPriceTableEntry == null) { throw new InvalidOperationException(PRICE_TABLE_ENTRY_NOT_CREATED); } GetMaterialPriceModelView createdPriceModelView = new GetMaterialPriceModelView(); createdPriceModelView.materialId = material.Id; createdPriceModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(savedMaterialPriceTableEntry.timePeriod.startingDate); createdPriceModelView.endingDate = LocalDateTimePattern.GeneralIso.Format(savedMaterialPriceTableEntry.timePeriod.endingDate); createdPriceModelView.value = savedMaterialPriceTableEntry.price.value; createdPriceModelView.currency = defaultCurrency; createdPriceModelView.area = defaultArea; createdPriceModelView.id = savedMaterialPriceTableEntry.Id; return(createdPriceModelView); }
/// <summary> /// Updates a material's price table entry /// </summary> /// <param name="modelView">model view with the update information</param> /// <returns></returns> public static GetMaterialPriceModelView update(UpdatePriceTableEntryModelView modelView, IHttpClientFactory clientFactory) { string defaultCurrency = CurrencyPerAreaConversionService.getBaseCurrency(); string defaultArea = CurrencyPerAreaConversionService.getBaseArea(); MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository(); long materialId = modelView.entityId; bool performedAtLeastOneUpdate = false; Material material = materialRepository.find(materialId); if (material == null) { throw new ResourceNotFoundException(MATERIAL_NOT_FOUND); } MaterialPriceTableRepository materialPriceTableRepository = PersistenceContext.repositories().createMaterialPriceTableRepository(); long materialPriceTableEntryId = modelView.tableEntryId; IEnumerable <MaterialPriceTableEntry> allEntries = materialPriceTableRepository.findAll(); if (allEntries == null || !allEntries.Any()) { throw new ResourceNotFoundException(NO_ENTRIES_FOUND); } MaterialPriceTableEntry tableEntryToUpdate = materialPriceTableRepository.find(materialPriceTableEntryId); if (tableEntryToUpdate == null) { throw new ResourceNotFoundException(ENTRY_NOT_FOUND); } if (tableEntryToUpdate.entity.Id != modelView.entityId) { throw new InvalidOperationException(ENTRY_DOESNT_BELONG_TO_MATERIAL); } LocalDateTime currentTime = NodaTime.LocalDateTime.FromDateTime(SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc()); if (modelView.priceTableEntry.endingDate != null && !LocalDateTimePattern.GeneralIso.Format(tableEntryToUpdate.timePeriod.startingDate).Equals(modelView.priceTableEntry.startingDate)) { if (tableEntryToUpdate.timePeriod.startingDate.CompareTo(currentTime) < 0) { throw new InvalidOperationException(PAST_DATE); } } if (modelView.priceTableEntry.startingDate != null && !LocalDateTimePattern.GeneralIso.Format(tableEntryToUpdate.timePeriod.endingDate).Equals(modelView.priceTableEntry.endingDate)) { if (tableEntryToUpdate.timePeriod.endingDate.CompareTo(currentTime) < 0) { throw new InvalidOperationException(PAST_DATE); } } if (modelView.priceTableEntry.price != null) { CurrenciesService.checkCurrencySupport(modelView.priceTableEntry.price.currency); AreasService.checkAreaSupport(modelView.priceTableEntry.price.area); Price newPrice = null; try { if (defaultCurrency.Equals(modelView.priceTableEntry.price.currency) && defaultArea.Equals(modelView.priceTableEntry.price.area)) { newPrice = Price.valueOf(modelView.priceTableEntry.price.value); } else { Task <double> convertedValueTask = new CurrencyPerAreaConversionService(clientFactory) .convertCurrencyPerAreaToDefaultCurrencyPerArea( modelView.priceTableEntry.price.currency, modelView.priceTableEntry.price.area, modelView.priceTableEntry.price.value); convertedValueTask.Wait(); double convertedValue = convertedValueTask.Result; newPrice = Price.valueOf(convertedValue); } } catch (HttpRequestException) { newPrice = Price.valueOf(modelView.priceTableEntry.price.value); } tableEntryToUpdate.changePrice(newPrice); performedAtLeastOneUpdate = true; } if (modelView.priceTableEntry.endingDate != null) { LocalDateTime newEndingDate; try { string newEndingDateAsString = modelView.priceTableEntry.endingDate; newEndingDate = LocalDateTimePattern.GeneralIso.Parse(newEndingDateAsString).GetValueOrThrow(); tableEntryToUpdate.changeTimePeriod(TimePeriod.valueOf(tableEntryToUpdate.timePeriod.startingDate, newEndingDate)); performedAtLeastOneUpdate = true; } catch (UnparsableValueException) { throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText); } } if (modelView.priceTableEntry.startingDate != null) { LocalDateTime newStartingDate; try { string newStartingDateAsString = modelView.priceTableEntry.startingDate; newStartingDate = LocalDateTimePattern.GeneralIso.Parse(newStartingDateAsString).GetValueOrThrow(); tableEntryToUpdate.changeTimePeriod(TimePeriod.valueOf(newStartingDate, tableEntryToUpdate.timePeriod.endingDate)); performedAtLeastOneUpdate = true; } catch (UnparsableValueException) { throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText); } } if (performedAtLeastOneUpdate) { MaterialPriceTableEntry updatedTableEntry = materialPriceTableRepository.update(tableEntryToUpdate); if (updatedTableEntry == null) { throw new InvalidOperationException(UPDATE_NOT_SUCCESSFUL); } GetMaterialPriceModelView updatedTableEntryModelView = new GetMaterialPriceModelView(); updatedTableEntryModelView.id = updatedTableEntry.Id; updatedTableEntryModelView.materialId = updatedTableEntry.entity.Id; updatedTableEntryModelView.value = updatedTableEntry.price.value; updatedTableEntryModelView.currency = defaultCurrency; updatedTableEntryModelView.area = defaultArea; updatedTableEntryModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(updatedTableEntry.timePeriod.startingDate); updatedTableEntryModelView.endingDate = LocalDateTimePattern.GeneralIso.Format(updatedTableEntry.timePeriod.endingDate); return(updatedTableEntryModelView); } throw new InvalidOperationException(UPDATE_NOT_SUCCESSFUL); }