コード例 #1
0
/*         [Fact, TestPriority(-8)]
 *      public async void ensureGetAllMaterialsPriceHistoryReturnsOkIfTheresContent()
 *      {
 *          string testNumber = "-8";
 *
 *          MaterialDTO createdMaterial = await createMaterial(testNumber + " first material");
 *          MaterialDTO otherCreatedMaterial = await createMaterial(testNumber + " second material");
 *          AddPriceTableEntryModelView addPriceTableEntry =
 *              createPriceTableEntry(-8);
 *
 *
 *          var createMaterialPriceTableEntry =
 *              await httpClient.PostAsJsonAsync
 *              (
 *                  BASE_URI + "/materials/" + createdMaterial.id,
 *                  addPriceTableEntry
 *              );
 *
 *          var otherCreateMaterialPriceTableEntry =
 *              await httpClient.PostAsJsonAsync
 *              (
 *                  BASE_URI + "/materials/" + otherCreatedMaterial.id,
 *                  addPriceTableEntry
 *              );
 *
 *          Assert.Equal(HttpStatusCode.Created, otherCreateMaterialPriceTableEntry.StatusCode);
 *          //TODO Check why the creation of this entry fails
 *          //Assert.Equal(HttpStatusCode.Created, createMaterialPriceTableEntry.StatusCode);
 *
 *          var getAll = await httpClient.GetAsync(BASE_URI + "/materials");
 *
 *          Assert.Equal(HttpStatusCode.OK, getAll.StatusCode);
 *
 *          GetAllMaterialPriceHistoryModelView responseContent =
 *              await getAll.Content.ReadAsAsync<GetAllMaterialPriceHistoryModelView>();
 *
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.value,
 *                      responseContent[0].value);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.currency,
 *                      responseContent[0].currency);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.area,
 *                      responseContent[0].area);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.startingDate,
 *                      responseContent[0].startingDate);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.endingDate,
 *                      responseContent[0].endingDate);
 *          Assert.Equal(otherCreatedMaterial.id, responseContent[0].materialId);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.value,
 *                      responseContent[1].value);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.currency,
 *                      responseContent[1].currency);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.price.area,
 *                      responseContent[1].area);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.startingDate,
 *                      responseContent[1].startingDate);
 *          Assert.Equal(addPriceTableEntry.priceTableEntry.endingDate,
 *                      responseContent[1].endingDate);
 *          Assert.Equal(otherCreatedMaterial.id, responseContent[1].materialId);
 *      } */

/*         [Fact, TestPriority(-7)]
 *      public async void ensureGetMaterialPriceHistoryByIdReturnsNotFoundForNonExistingId()
 *      {
 *          string testNumber = "-7";
 *
 *          MaterialDTO otherCreatedMaterial = await createMaterial(testNumber);
 *          AddPriceTableEntryModelView addPriceTableEntry =
 *              createPriceTableEntry(-7);
 *
 *          var otherCreateMaterialPriceTableEntry =
 *              await httpClient.PostAsJsonAsync
 *              (
 *                  BASE_URI + "/materials/" + otherCreatedMaterial.id,
 *                  addPriceTableEntry
 *              );
 *
 *          Assert.Equal(HttpStatusCode.Created, otherCreateMaterialPriceTableEntry.StatusCode);
 *
 *          var getById =
 *              await httpClient.GetAsync(BASE_URI + "/materials/" + otherCreatedMaterial.id + 1);
 *
 *          Assert.Equal(HttpStatusCode.NotFound, getById.StatusCode);
 *      } */

        /*  [Fact, TestPriority(-6)]
         * public async void ensureGetMaterialPriceHistoryByIdReturnsOk()
         * {
         *   string testNumber = "-6";
         *
         *   MaterialDTO otherCreatedMaterial = await createMaterial(testNumber);
         *   AddPriceTableEntryModelView addPriceTableEntry =
         *       createPriceTableEntry(-6);
         *
         *   var otherCreateMaterialPriceTableEntry =
         *       await httpClient.PostAsJsonAsync
         *       (
         *           BASE_URI + "/materials/" + otherCreatedMaterial.id,
         *           addPriceTableEntry
         *       );
         *
         *   Assert.Equal(HttpStatusCode.Created, otherCreateMaterialPriceTableEntry.StatusCode);
         *
         *   var getById =
         *       await httpClient.GetAsync(BASE_URI + "/materials/" + otherCreatedMaterial.id);
         *
         *   Assert.Equal(HttpStatusCode.OK, getById.StatusCode);
         *
         *   GetAllMaterialPriceHistoryModelView responseContent =
         *       await getById.Content.ReadAsAsync<GetAllMaterialPriceHistoryModelView>();
         *
         *   assertPriceTableEntry(addPriceTableEntry, responseContent[0]);
         * }
         *
         * private void assertPriceTableEntry(AddPriceTableEntryModelView expectedModelView, GetMaterialPriceModelView actualModelView)
         * {
         *   Assert.Equal(expectedModelView.priceTableEntry.startingDate,
         *               actualModelView.startingDate);
         *   Assert.Equal(expectedModelView.priceTableEntry.endingDate,
         *               actualModelView.endingDate);
         *   Assert.Equal(expectedModelView.priceTableEntry.price.currency,
         *               actualModelView.currency);
         *   Assert.Equal(expectedModelView.priceTableEntry.price.area,
         *               actualModelView.area);
         *   Assert.Equal(expectedModelView.priceTableEntry.price.value,
         *               actualModelView.value);
         * } */

        private AddPriceTableEntryModelView createPriceTableEntry(double testNumber)
        {
            AddPriceTableEntryModelView priceTableEntry =
                new AddPriceTableEntryModelView();

            priceTableEntry.priceTableEntry                = new PriceTableEntryDTO();
            priceTableEntry.priceTableEntry.price          = new PriceDTO();
            priceTableEntry.priceTableEntry.price.currency = "EUR";
            priceTableEntry.priceTableEntry.price.value    = 1000 + testNumber;
            priceTableEntry.priceTableEntry.price.area     = "m2";
            double year = 2016 - testNumber;

            priceTableEntry.priceTableEntry.startingDate = year + "-01-22T12:04:00";
            priceTableEntry.priceTableEntry.endingDate   = year + "-05-22T12:04:00";

            return(priceTableEntry);
        }
コード例 #2
0
 public ActionResult addMaterialPriceTableEntry(long id, [FromBody] AddPriceTableEntryModelView modelView)
 {
     try
     {
         modelView.entityId = id;
         GetMaterialPriceModelView createdPrice = new core.application.PriceTablesController().addMaterialPriceTableEntry(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 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);
        }
コード例 #4
0
 /// <summary>
 /// Adds a new price table entry for a material
 /// </summary>
 /// <param name="modelView">model view with the price table entry's information</param>
 public GetMaterialPriceModelView addMaterialPriceTableEntry(AddPriceTableEntryModelView modelView, IHttpClientFactory clientFactory)
 {
     return(AddMaterialPriceTableEntryService.create(modelView, clientFactory));
 }