public ActionResult updateFinishPriceTableEntry(long materialid, long finishid, long entryid, [FromBody] UpdateFinishPriceTableEntryModelView modelView)
 {
     try
     {
         modelView.entityId     = materialid;
         modelView.finishId     = finishid;
         modelView.tableEntryId = entryid;
         GetMaterialFinishPriceModelView updatedEntry = new core.application.PriceTablesController().updateFinishPriceTableEntry(modelView, clientFactory);
         if (updatedEntry == null)
         {
             return(BadRequest(new { error = ENTRY_NOT_UPDATED }));
         }
         return(Ok(updatedEntry));
     }
     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>
        /// Updates a finish's price table entry
        /// </summary>
        /// <param name="modelView">model view containing updatable information</param>
        /// <returns>True if the update is successful</returns>
        public static GetMaterialFinishPriceModelView update(UpdateFinishPriceTableEntryModelView 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);
            }

            foreach (Finish finish in material.Finishes)
            {
                if (finish.Id == modelView.finishId)
                {
                    FinishPriceTableRepository finishPriceTableRepository = PersistenceContext.repositories().createFinishPriceTableRepository();
                    long finishPriceTableEntryId = modelView.tableEntryId;

                    FinishPriceTableEntry tableEntryToUpdate = finishPriceTableRepository.find(finishPriceTableEntryId);

                    if (tableEntryToUpdate == null)
                    {
                        throw new ResourceNotFoundException(ENTRY_NOT_FOUND);
                    }

                    if (tableEntryToUpdate.entity.Id != modelView.finishId)
                    {
                        throw new InvalidOperationException(ENTRY_DOESNT_BELONG_TO_FINISH);
                    }

                    LocalDateTime currentTime = NodaTime.LocalDateTime.FromDateTime(SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc());

                    if (modelView.priceTableEntry.startingDate != 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.endingDate != 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)
                    {
                        FinishPriceTableEntry updatedTableEntry = finishPriceTableRepository.update(tableEntryToUpdate);

                        if (updatedTableEntry == null)
                        {
                            throw new InvalidOperationException(UPDATE_NOT_SUCCESSFUL);
                        }

                        GetMaterialFinishPriceModelView updatedTableEntryModelView = new GetMaterialFinishPriceModelView();

                        updatedTableEntryModelView.id           = updatedTableEntry.Id;
                        updatedTableEntryModelView.finishId     = 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 ResourceNotFoundException(FINISH_NOT_FOUND_OR_DOESNT_BELONG_TO_MATERIAL);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Updates a finish's price table entry
 /// </summary>
 /// <param name="modelView">model view with the necessary update information</param>
 public GetMaterialFinishPriceModelView updateFinishPriceTableEntry(UpdateFinishPriceTableEntryModelView modelView, IHttpClientFactory clientFactory)
 {
     return(UpdateFinishPriceTableEntryService.update(modelView, clientFactory));
 }