Exemplo n.º 1
0
        public async Task <ProductCreationDto.GasRefillDto> CreateGasRefill(ProductCreationDto.GasRefillDto model, string userId)
        {
            var gass = await _context.Products.FirstOrDefaultAsync(x => x.Name == model.Name);

            if (gass == null)
            {
                Product productToCreate = new Product
                {
                    Category       = model.Category,
                    Name           = model.Name,
                    Amount         = model.Amount,
                    Description    = model.Description,
                    ConvinienceFee = model.ConvinienceFee,
                    Images         = JsonConvert.SerializeObject(model.UploadedImageList),
                    UserId         = userId
                };
                _context.Products.Add(productToCreate);
                if (await _context.SaveChangesAsync() > 0)
                {
                    return(model);
                }
                return(null);
            }
            return(null);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> RefillGass(ProductCreationDto.GasRefillDto productDto, string userId)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new Response <string>
                    {
                        Code = "400",
                        Message = "Invalid model"
                    }));
                }
                if (userId != User.FindFirst("id").Value)
                {
                    return(Unauthorized());
                }


                ProductCreationDto.GasRefillDto refilledGass = await _productManager.CreateGasRefill(productDto, userId);

                if (refilledGass != null)
                {
                    return(Created("", new Response <ProductCreationDto.GasRefillDto>
                    {
                        Code = "201",
                        Message = $"You have successfully created a product with name {refilledGass.Name}",
                        Data = refilledGass
                    }));
                }

                return(BadRequest(new Response <string> {
                    Code = "500", Message = "Internal server error"
                }));
            }
            catch (Exception ex)
            {
                var err = ex.Message;
                return(BadRequest(new Response <string> {
                    Code = "500", Message = "Internal server error"
                }));
            }
        }