public async Task <ActionResult <ConfiguredLaptopModel> > Post(ConfiguredLaptopModel configuredLaptopModel)
        {
            var response = await _configurationService.PostConfiguredLaptop(configuredLaptopModel);

            if (response.IsError)
            {
                return(StatusCode((int)response.ErrorCode, response.ErrorDescription));
            }

            return(Ok(response.Data));
        }
Example #2
0
        public async Task <ServiceResponse <ConfiguredLaptopModel> > PostConfiguredLaptop(ConfiguredLaptopModel configuredLaptopModel)
        {
            try
            {
                var basketItems = await _context.ConfiguredLaptops.ToListAsync();


                var checkItem = await _context.ConfiguredLaptops
                                .Include(i => i.ConfigurationItems)
                                //.Include(i => i.Laptop)
                                .FirstOrDefaultAsync(f =>
                                                     f.LaptopId == configuredLaptopModel.Laptop.Id);


                //f.ConfigurationItems.Equals(configuredLaptopModel.ConfigurationItems));

                var entity = _mapper.Map <ConfiguredLaptop>(configuredLaptopModel);

                if (checkItem != null)
                {
                    if (checkItem.LaptopId.Equals(entity.LaptopId) && checkItem.ConfigurationItems.Equals(entity.ConfigurationItems))
                    {
                    }

                    return(new ServiceResponse <ConfiguredLaptopModel>
                    {
                        ErrorCode = HttpStatusCode.Conflict,
                        ErrorDescription = "Laptop Brand with same configuration already added to basket"
                    });
                }



                _context.Entry(entity).State = EntityState.Added;

                if (await _context.SaveChangesAsync() > 0)
                {
                    return(new ServiceResponse <ConfiguredLaptopModel>
                    {
                        Data = configuredLaptopModel
                    });
                }

                return(new ServiceResponse <ConfiguredLaptopModel>
                {
                    ErrorCode = HttpStatusCode.InternalServerError,
                    ErrorDescription = "Internal Server Error"
                });
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(new ServiceResponse <ConfiguredLaptopModel>
                {
                    ErrorCode = HttpStatusCode.InternalServerError,
                    ErrorDescription = "Internal Server Error"
                });
            }
        }