Esempio n. 1
0
        public async Task <BaseResponse> InsertMobile(InsertMobileModel model)
        {
            BaseResponse response = null;

            try
            {
                var repo = (_mobilieRepository as MobileRepository);

                response = BaseResponse.Ok();

                var mobile = new Mobile
                {
                    Brand            = model.Brand,
                    CPU              = model.CPU,
                    InternalMemory   = model.InternalMemory,
                    Name             = model.Name,
                    OS               = model.OS,
                    Price            = model.Price,
                    RAM              = model.RAM,
                    ScreenResolution = model.ScreenResolution,
                    ScreenSize       = model.ScreenSize,
                    Size             = model.Size,
                    VideoUrl         = model.VideoUrl,
                    Weight           = model.Weight
                };

                foreach (var item in model.Photos)
                {
                    var photo = new Domain.DAO.Entities.Photo()
                    {
                        Value = item.Base64
                    };
                    mobile.Photos.Add(photo);
                }

                repo.Add(mobile);

                repo.Save();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                response = BaseResponse.Fail($"Could not add item");
            }

            return(response);
        }
Esempio n. 2
0
        public async Task <BaseResponse> UpdateMobile(MobileListItem model)
        {
            BaseResponse response = null;

            try
            {
                var repo = (_mobilieRepository as MobileRepository);
                response = BaseResponse.Ok();

                var existingMobile = repo.FindBy(m => m.Id == model.MobileId)
                                     .Include(p => p.Photos)
                                     .SingleOrDefault();


                if (existingMobile != null)
                {
                    var mobile = new Mobile
                    {
                        Id               = model.MobileId,
                        Brand            = model.Brand,
                        CPU              = model.CPU,
                        InternalMemory   = model.InternalMemory,
                        Name             = model.Name,
                        OS               = model.OS,
                        Price            = model.Price,
                        RAM              = model.RAM,
                        ScreenResolution = model.ScreenResolution,
                        ScreenSize       = model.ScreenSize,
                        Size             = model.Size,
                        VideoUrl         = model.VideoUrl,
                        Weight           = model.Weight
                    };

                    repo.Context.Entry(existingMobile).CurrentValues.SetValues(mobile);

                    foreach (var existingPhoto in existingMobile.Photos.ToList())
                    {
                        if (model.Photos.Any(p => p.PhotoId == existingPhoto.Id))
                        {
                            repo.Context.Photos.Remove(existingPhoto);
                        }
                    }

                    foreach (var photo in model.Photos)
                    {
                        var existingPhoto = existingMobile.Photos
                                            .Where(p => p.Id == photo.PhotoId)
                                            .SingleOrDefault();

                        if (existingPhoto != null)
                        {
                            repo.Context.Entry(existingPhoto).CurrentValues.SetValues(photo);
                        }
                        else
                        {
                            var newPhoto = new Domain.DAO.Entities.Photo
                            {
                                Value = photo.Base64
                            };

                            existingMobile.Photos.Add(newPhoto);
                        }
                    }

                    repo.Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                response = BaseResponse.Fail($"Could not update item");
            }

            return(response);
        }