Example #1
0
        public async Task <ProductDto> SetResellPriceofProducts(Guid productID, double resellPrice)
        {
            try
            {
                _logger.LogInformation("Getting products with product ID.");
                var product = await _dbcontext.Products.Where(g => g.ProductID == productID).FirstOrDefaultAsync();

                if (product != null && resellPrice >= 0)
                {
                    _logger.LogInformation($"Setting the resell price of product with product ID {product.ProductID} to {resellPrice}");
                    product.ResellPrice = resellPrice;
                    _dbcontext.Products.Update(product);

                    var getResellHistory = new ResellHistory
                    {
                        ID          = Guid.NewGuid(),
                        ProductID   = productID,
                        ResellPrice = resellPrice,
                        DateTime    = DateTime.UtcNow
                    };
                    _dbcontext.ResellHistories.Add(getResellHistory);
                    await _dbcontext.SaveChangesAsync();
                }
                _logger.LogInformation($"Returns product with product ID {product.ProductID} with new resell price {product.ResellPrice}");
                return(await Task.FromResult(product));
            } catch (Exception ex)
            {
                _logger.LogInformation($"Error message: {ex.Message}");
                return(null);
            }
        }
Example #2
0
        public async Task <ActionResult> UpdateResellHistory([FromBody] ResellHistory dto)
        {
            if (dto.productId == Guid.Empty)
            {
                return(BadRequest());
            }

            try
            {
                dto.created = DateTime.Now;
                dto.Id      = Guid.NewGuid();
                _context.ResellHistory.Add(dto);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
Example #3
0
        public async Task <ActionResult> SetResellPrice(Guid prodID, [FromBody] decimal price)
        {
            var userRole = User.Claims.FirstOrDefault(x => x.Type == "role")?.Value;

            var product = await _context.Products.FindAsync(prodID);

            if (product == null)
            {
                return(NotFound());
            }

            try
            {
                ResellHistory newModel = new ResellHistory()
                {
                    Id        = new Guid(),
                    productId = prodID,
                    oldPrice  = product.Price,
                    newPrice  = price,
                    created   = DateTime.Now
                };

                product.Price = price;
                _context.Products.Update(product);
                await _context.SaveChangesAsync();

                _context.ResellHistory.Add(newModel);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }