public ActionResult addFinishPriceTableEntry(long materialid, long finishid, [FromBody] AddFinishPriceTableEntryModelView modelView) { try { modelView.entityId = materialid; modelView.finishId = finishid; GetMaterialFinishPriceModelView createdPrice = new core.application.PriceTablesController().addFinishPriceTableEntry(modelView, clientFactory); if (createdPrice == null) { return(BadRequest(new SimpleJSONMessageService(PRICE_ENTRY_NOT_CREATED))); } return(Created(Request.Path, createdPrice)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (NullReferenceException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (InvalidOperationException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (UnparsableValueException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (ArgumentException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Transforms and creates a finish price table entry /// </summary> /// <param name="modelView">model view with the necessary info to create a finish price table entry</param> /// <param name="clientFactory">injected client factory</param> /// <returns></returns> public static GetMaterialFinishPriceModelView create(AddFinishPriceTableEntryModelView modelView, IHttpClientFactory clientFactory) { string defaultCurrency = CurrencyPerAreaConversionService.getBaseCurrency(); string defaultArea = CurrencyPerAreaConversionService.getBaseArea(); MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository(); long materialId = modelView.entityId; Material material = materialRepository.find(materialId); if (material == null) { throw new ResourceNotFoundException(MATERIAL_NOT_FOUND); } //TODO Is this null check enough? Should we check ,if an entry exists, that the time period of the price entry is valid? MaterialPriceTableEntry materialPriceTableEntry = PersistenceContext.repositories().createMaterialPriceTableRepository().find(materialId); if (materialPriceTableEntry == null) { throw new InvalidOperationException(MATERIAL_HAS_NO_PRICE); } foreach (Finish finish in material.Finishes) { if (finish.Id == modelView.finishId) { 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); } FinishPriceTableEntry finishPriceTableEntry = new FinishPriceTableEntry(material.id(), finish, price, timePeriod); FinishPriceTableEntry savedFinishPriceTableEntry = PersistenceContext.repositories() .createFinishPriceTableRepository().save(finishPriceTableEntry); if (savedFinishPriceTableEntry == null) { throw new InvalidOperationException(PRICE_TABLE_ENTRY_NOT_CREATED); } GetMaterialFinishPriceModelView createdPriceModelView = new GetMaterialFinishPriceModelView(); createdPriceModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(savedFinishPriceTableEntry.timePeriod.startingDate); createdPriceModelView.endingDate = LocalDateTimePattern.GeneralIso.Format(savedFinishPriceTableEntry.timePeriod.endingDate); createdPriceModelView.value = savedFinishPriceTableEntry.price.value; createdPriceModelView.currency = defaultCurrency; createdPriceModelView.area = defaultArea; createdPriceModelView.finishId = finish.Id; createdPriceModelView.id = savedFinishPriceTableEntry.Id; return(createdPriceModelView); } } throw new ResourceNotFoundException(FINISH_NOT_FOUND); }
/// <summary> /// Adds new price table entry for a material's finish /// </summary> /// <param name="modelView">model view with the price table entry's information</param> public GetMaterialFinishPriceModelView addFinishPriceTableEntry(AddFinishPriceTableEntryModelView modelView, IHttpClientFactory clientFactory) { return(AddFinishPriceTableEntryService.create(modelView, clientFactory)); }