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));
            }
        }
Exemplo n.º 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));
            }
        }