public ActionResult <PastPrice> CreatePastPrice([FromBody] PastPriceCreationDto pastPriceCreationDto, [FromHeader] string key)
        {
            try
            {
                if (!auth.AuthorizeUser(key))
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
                }

                PastPrice pastPrice = mapper.Map <PastPrice>(pastPriceCreationDto);

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

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

                pastPriceRepository.CreatePastPrice(pastPrice);
                pastPriceRepository.SaveChanges();

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

                var location = linkGenerator.GetPathByAction("GetPastPriceById", "PastPrice", new { ItemId = pastPrice.ItemId });
                return(Created(location, pastPrice));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Create error", null);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Пример #2
0
        public ActionResult <PastPrice> CreatePastPrice([FromBody] PastPriceCreationDto pastPriceCreationDto, [FromHeader] string key)
        {
            try
            {
                //pristup metodi imaju samo autorizovani korisnici
                if (!auth.AuthorizeUser(key))
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
                }

                PastPrice pastPrice = mapper.Map <PastPrice>(pastPriceCreationDto);

                Product product = productRepository.GetProductById(pastPrice.ItemForSaleId);
                Service service = serviceRepository.GetServiceById(pastPrice.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.CreatePastPrice(pastPrice);
                pastPriceRepository.SaveChanges();

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

                var location = linkGenerator.GetPathByAction("GetPastPriceById", "PastPrice", new { ItemForSaleId = pastPrice.ItemForSaleId });
                return(Created(location, pastPrice));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Create error", null);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
 //sacuvam staru cenu nakon promene
 public void UpdateService(Service oldService, Service newService)
 {
     oldService.Name        = newService.Name;
     oldService.Description = newService.Description;
     oldService.AccountId   = newService.AccountId;
     if (oldService.Price != newService.Price)
     {
         PastPrice pastPrice = new PastPrice();
         pastPrice.Price         = oldService.Price;
         pastPrice.ItemForSaleId = oldService.ItemForSaleId;
         oldService.Price        = newService.Price;
         pastPriceRepository.CreatePastPrice(pastPrice);
     }
 }
 //sacuvam staru cenu nakon promene
 public void UpdateProduct(Product oldProduct, Product newProduct)
 {
     oldProduct.Name        = newProduct.Name;
     oldProduct.Description = newProduct.Description;
     oldProduct.AccountId   = newProduct.AccountId;
     oldProduct.Weight      = newProduct.Weight;
     if (oldProduct.Price != newProduct.Price)
     {
         PastPrice pastPrice = new PastPrice();
         pastPrice.Price         = oldProduct.Price;
         pastPrice.ItemForSaleId = oldProduct.ItemForSaleId;
         oldProduct.Price        = newProduct.Price;
         pastPriceRepository.CreatePastPrice(pastPrice);
     }
 }
        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));
            }
        }
 public void UpdatePastPrice(PastPrice oldPastPrice, PastPrice newPastPrice)
 {
     oldPastPrice.Price  = newPastPrice.Price;
     oldPastPrice.ItemId = newPastPrice.ItemId;
 }
 public void CreatePastPrice(PastPrice pastPrice)
 {
     context.PastPrices.Add(pastPrice);
 }