コード例 #1
0
        public ActionResult <PastPrice> UpdatePastPrice([FromBody] PastPriceDto pastPriceDto, int pastPriceId, [FromHeader] string key)
        {
            try
            {
                //pristup metodi imaju samo autorizovani korisnici
                if (!auth.AuthorizeUser(key))
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
                }

                var oldPastPrice = pastPriceRepository.GetPastPriceById(pastPriceId);
                if (oldPastPrice == null)
                {
                    return(NotFound());
                }
                PastPrice newPastPrice = mapper.Map <PastPrice>(pastPriceDto);

                Product product = productRepository.GetProductById(newPastPrice.ItemForSaleId);
                Service service = serviceRepository.GetServiceById(newPastPrice.ItemForSaleId);

                //prosla cena mora da se odnosi na neki proizvod ili uslugu koji vec postoje
                if (product == null && service == null)
                {
                    throw new DbException("Item for sale with that id does not exists!");
                }

                pastPriceRepository.UpdatePastPrice(oldPastPrice, newPastPrice);
                pastPriceRepository.SaveChanges();

                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "Updated past price", null);

                return(Ok(oldPastPrice));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Update error", null);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        public ActionResult <PastPrice> UpdatePastPrice([FromBody] PastPriceDto pastPriceDto, int pastPriceId, [FromHeader] string key)
        {
            try
            {
                if (!auth.AuthorizeUser(key))
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
                }

                var oldPastPrice = pastPriceRepository.GetPastPriceById(pastPriceId);
                if (oldPastPrice == null)
                {
                    return(NotFound());
                }
                PastPrice newPastPrice = mapper.Map <PastPrice>(pastPriceDto);

                Product product = productRepository.GetProductById(newPastPrice.ItemId);
                Service service = serviceRepository.GetServiceById(newPastPrice.ItemId);


                if (product == null && service == null)
                {
                    throw new DatabaseException("Item with that id does not exists!");
                }

                pastPriceRepository.UpdatePastPrice(oldPastPrice, newPastPrice);
                pastPriceRepository.SaveChanges();

                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "Updated past price", null);

                return(Ok(oldPastPrice));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Update error", null);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }