public ActionResult fetchCurrentMaterialPrice(long materialId, [FromQuery] string currency, [FromQuery] string area)
        {
            GetCurrentMaterialPriceModelView fetchCurrentMaterialPrice = new GetCurrentMaterialPriceModelView();

            fetchCurrentMaterialPrice.material              = new GetBasicMaterialModelView();
            fetchCurrentMaterialPrice.currentPrice          = new PriceModelView();
            fetchCurrentMaterialPrice.material.id           = materialId;
            fetchCurrentMaterialPrice.currentPrice.currency = currency;
            fetchCurrentMaterialPrice.currentPrice.area     = area;
            try
            {
                GetCurrentMaterialPriceModelView currentMaterialPrice = new core.application.PriceTablesController().fetchCurrentMaterialPrice(fetchCurrentMaterialPrice, clientFactory);
                return(Ok(currentMaterialPrice));
            }
            catch (ResourceNotFoundException e)
            {
                return(NotFound(new SimpleJSONMessageService(e.Message)));
            }
            catch (ArgumentException e)
            {
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            }
            catch (Exception)
            {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
        /// <summary>
        /// Fetches the current price of a material
        /// </summary>
        /// <param name="modelView">ModelView with the necessary information to fetch a material's current price</param>
        /// <returns>GetCurrentMaterialPriceModelView with a material's current price</returns>
        public static GetCurrentMaterialPriceModelView fromMaterial(GetCurrentMaterialPriceModelView modelView, IHttpClientFactory clientFactory)
        {
            Material material = PersistenceContext.repositories().createMaterialRepository().find(modelView.material.id);

            if (material == null)
            {
                throw new ResourceNotFoundException(string.Format(MATERIAL_NOT_FOUND, modelView.material.id));
            }

            MaterialPriceTableEntry currentPrice = PersistenceContext.repositories().createMaterialPriceTableRepository().fetchCurrentMaterialPrice(modelView.material.id);

            if (currentPrice == null)
            {
                throw new ResourceNotFoundException(string.Format(NO_CURRENT_MATERIAL_PRICE, modelView.material.id));
            }

            GetCurrentMaterialPriceModelView currentMaterialPriceModelView = new GetCurrentMaterialPriceModelView();

            currentMaterialPriceModelView.material                = new GetBasicMaterialModelView();
            currentMaterialPriceModelView.currentPrice            = new PriceModelView();
            currentMaterialPriceModelView.timePeriod              = new TimePeriodModelView();
            currentMaterialPriceModelView.tableEntryId            = currentPrice.Id;
            currentMaterialPriceModelView.material.id             = material.Id;
            currentMaterialPriceModelView.material.designation    = material.designation;
            currentMaterialPriceModelView.material.reference      = material.reference;
            currentMaterialPriceModelView.material.imageFilename  = material.image;
            currentMaterialPriceModelView.timePeriod.startingDate = LocalDateTimePattern.GeneralIso.Format(currentPrice.timePeriod.startingDate);
            currentMaterialPriceModelView.timePeriod.endingDate   = LocalDateTimePattern.GeneralIso.Format(currentPrice.timePeriod.endingDate);
            if (modelView.currentPrice.currency == null || modelView.currentPrice.area == null)
            {
                currentMaterialPriceModelView.currentPrice.value    = currentPrice.price.value;
                currentMaterialPriceModelView.currentPrice.area     = CurrencyPerAreaConversionService.getBaseArea();
                currentMaterialPriceModelView.currentPrice.currency = CurrencyPerAreaConversionService.getBaseCurrency();
            }
            else
            {
                Task <double> convertedValueTask =
                    new CurrencyPerAreaConversionService(clientFactory)
                    .convertDefaultCurrencyPerAreaToCurrencyPerArea(currentPrice.price.value, modelView.currentPrice.currency, modelView.currentPrice.area);
                convertedValueTask.Wait();
                currentMaterialPriceModelView.currentPrice.value    = convertedValueTask.Result;
                currentMaterialPriceModelView.currentPrice.currency = modelView.currentPrice.currency;
                currentMaterialPriceModelView.currentPrice.area     = modelView.currentPrice.area;
            }

            return(currentMaterialPriceModelView);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Fetches the current price of a material
 /// </summary>
 /// <param name="modelView">GetCurrentMaterialPriceModelView with info to fetch the current price</param>
 /// <returns>GetCurrentMaterialPriceModelView with the material's current price</returns>
 public GetCurrentMaterialPriceModelView fetchCurrentMaterialPrice(GetCurrentMaterialPriceModelView modelView, IHttpClientFactory clientFactory)
 {
     return(CurrentPriceService.fromMaterial(modelView, clientFactory));
 }