Beispiel #1
0
        public static ItemUpdateModel ToItemUpdateModel(Item dbModel, IEnumerable <Category> allDbCategories)
        {
            var updateModel = new ItemUpdateModel {
                Id               = dbModel.Id,
                Name             = dbModel.Name,
                ShortDescription = dbModel.ShortDescription,
                LongDescription  = dbModel.LongDescription,
                Price            = dbModel.Price,
                InStock          = dbModel.InStock,
                Categories       = new List <ItemNewCategoriesCheckBoxModel>(),
                OldImageUrl      = ToBase64String(dbModel.Image)
            };

            foreach (var category in allDbCategories)
            {
                var itemNewCategoriesCheckBoxModel = new ItemNewCategoriesCheckBoxModel {
                    Id      = category.Id,
                    Name    = category.Name,
                    Checked = false
                };

                //mark dbModel categories as checked
                if (dbModel.Categories.Select(x => x.Id).Contains(category.Id))
                {
                    itemNewCategoriesCheckBoxModel.Checked = true;
                }

                updateModel.Categories.Add(itemNewCategoriesCheckBoxModel);
            }

            return(updateModel);
        }
        public ItemUpdateModel PutUpdateItem(ItemUpdateModel itemUpdateModel,
                                             [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var user = GetAndValidateUser(sessionKey);

                var item = this.Data.Items.All().Where(it => it.UserId == user.Id).SingleOrDefault(it => it.Id == itemUpdateModel.Id);

                if (item.Title != itemUpdateModel.Title)
                {
                    item.Title = itemUpdateModel.Title;
                }

                if (item.ParentId != itemUpdateModel.ParentId)
                {
                    item.ParentId = itemUpdateModel.ParentId;
                }

                this.Data.SaveChanges();

                return(itemUpdateModel);
            });

            return(responseMsg);
        }
        public async Task CreateAsync_CategoryValidationFailed_ThrowsError()
        {
            // Arrange
            var fixture  = new Fixture();
            var item     = new ItemUpdateModel();
            var expected = fixture.Create <string>();

            var categoryGetService = new Mock <ICategoryGetService>();

            categoryGetService
            .Setup(x => x.ValidateAsync(item))
            .Throws(new InvalidOperationException(expected));

            var itemDataAccess = new Mock <IItemDataAccess>();

            var itemGetService = new ItemCreateService(itemDataAccess.Object, categoryGetService.Object);

            // Act
            var action = new Func <Task>(() => itemGetService.CreateAsync(item));

            // Assert
            await action.Should().ThrowAsync <InvalidOperationException>().WithMessage(expected);

            itemDataAccess.Verify(x => x.InsertAsync(item), Times.Never);
        }
        public async Task <ItemList.Domain.Item> InsertAsync(ItemUpdateModel item)
        {
            var result = await this.Context.AddAsync(this.Mapper.Map <Item>(item));

            await this.Context.SaveChangesAsync();

            return(this.Mapper.Map <ItemList.Domain.Item>(result.Entity));
        }
Beispiel #5
0
        public ItemUpdateModel UpdateItemModel(ItemUpdateModel updateModel)
        {
            Item item = _unitOfWork.Items.Get(updateModel.Id);

            item.Name     = updateModel.Name;
            item.Code     = updateModel.Code;
            item.Price    = updateModel.Price;
            item.Category = updateModel.Category;
            _unitOfWork.Items.Update(item);
            return(updateModel);
        }
Beispiel #6
0
 public IActionResult UpdateItem(Guid id,
                                 [FromBody] ItemUpdateModel itemUpdateModel)
 {
     if (itemUpdateModel == null || !ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     _itemService.UpdateItem(id, itemUpdateModel.Name, itemUpdateModel.Code,
                             itemUpdateModel.Price, itemUpdateModel.Category);
     return(NoContent());
 }
        public async Task <ItemList.Domain.Item> UpdateAsync(ItemUpdateModel item)
        {
            var existing = await this.Get(item);

            var result = this.Mapper.Map(item, existing);

            this.Context.Update(result);

            await this.Context.SaveChangesAsync();

            return(this.Mapper.Map <ItemList.Domain.Item>(result));
        }
        public async Task <IActionResult> ChangeProductQuantity(int productId, [FromBody] ItemUpdateModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound());
            }
            await _itemUseCases.UpdateItemQuantity(user, model.ProductQuantity, productId);

            return(NoContent());
        }
Beispiel #9
0
        public IActionResult UpdateItem([FromRoute] int itemId, [FromBody] ItemUpdateModel item)
        {
            userInformation.ThrowErrorIfNotLoggedIn();

            var newItem = new PantryItem
            {
                //Name = item.Name,
                Quantity = new Quantity
                {
                    Amount = item.Amount,
                    Unit   = item.MeasurementUnit
                },
                BuyDate        = item.BuyDate,
                ExpirationDate = item.ExpirationDate
            };

            return(Ok(service.UpdateItem(itemId, newItem)));
        }
Beispiel #10
0
        public IActionResult Update(int id, [FromBody] ItemUpdateModel model)
        {
            var Items = _mapper.Map <Items>(model);
            var user  = Utilities.getUserId(User);

            Items.Id = id;

            try
            {
                // update Items
                var Item = _ItemService.Update(Items, user);
                return(Ok(_mapper.Map <ItemModel>(Item)));
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
        public async Task CreateAsync_CategoryValidationSucceed_CreatesItem()
        {
            // Arrange
            var item     = new ItemUpdateModel();
            var expected = new Item();

            var categoryGetService = new Mock <ICategoryGetService>();

            categoryGetService.Setup(x => x.ValidateAsync(item));

            var itemDataAcces = new Mock <IItemDataAccess>();

            itemDataAcces.Setup(x => x.InsertAsync(item)).ReturnsAsync(expected);

            var itemGetService = new ItemCreateService(itemDataAcces.Object, categoryGetService.Object);

            // Act
            var result = await itemGetService.CreateAsync(item);

            // Assert
            result.Should().Be(expected);
        }
Beispiel #12
0
        async public Task <IActionResult> Update(ItemUpdateModel categoryUpdateModel)
        {
            if (ModelState.IsValid)
            {
                Console.WriteLine("Model valid!");
                var dbModel = await DataMapper.ModelMapper.ToItemDbModel(categoryUpdateModel, _categoryRepository);

                //To delete the many to many relationships between an item and a category
                //the easiest way is to delete the item and recreate it

                // var categories = (await _categoryRepository.Get(
                //     x => dbModel.Categories.Select(y => y.Id).Contains(x.Id),
                //     includeProperties:"Items")).ToList();

                // var categories = dbModel.Categories;
                //
                // foreach (var category in categories) {
                //     if (category.Items.Select(x => x.Id).Contains(dbModel.Id) == false) {
                //         var itemList = category.Items as HashSet<Item>;
                //         itemList?.Add(dbModel);
                //         category.Items = itemList;
                //         _categoryRepository.Update(category);
                //     }
                // }
                // _itemRepository.DetachEntity();
                _itemRepository.Update(dbModel);
                // _itemRepository.Update(dbModel);

                await _itemRepository.SaveChangesAsync();
            }
            else
            {
                Console.WriteLine("Model invalid!");
                return(View("Edit", categoryUpdateModel));
            }

            return(RedirectToAction("Index", "Item", new { id = categoryUpdateModel.Id }));
        }
Beispiel #13
0
        async public static Task <Item> ToItemDbModel(ItemUpdateModel updateModel, IRepository <Category> repository = null)
        {
            var dbModel = new Item {
                Id               = updateModel.Id,
                Name             = updateModel.Name,
                Price            = updateModel.Price,
                Image            = FromFileForm(updateModel.NewImage),
                Categories       = null,
                InStock          = updateModel.InStock,
                LongDescription  = updateModel.LongDescription,
                ShortDescription = updateModel.ShortDescription
            };

            if (dbModel.Image == null)
            {
                dbModel.Image = FromBase64String(updateModel.OldImageUrl);
            }

            if (repository != null && updateModel.Categories != null)
            {
                dbModel.Categories = new List <Category>();

                foreach (var category in updateModel.Categories)
                {
                    if (category.Checked)
                    {
                        var categoryDb = (await repository.Get(x => x.Id == category.Id)).First();
                        if (categoryDb != null)
                        {
                            dbModel.Categories.Add(categoryDb);
                        }
                    }
                }
            }

            return(dbModel);
        }
Beispiel #14
0
        public IActionResult Update(int id, [FromBody] ItemUpdateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // map model to entity and set id
            var item = _mapper.Map <Item>(model);

            item.Id = id;

            try
            {
                // update item
                _itemService.Update(item);
                return(Ok());
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Beispiel #15
0
        //public async Task<OperationDataResult<ItemModel>> GetItemById(int itemId)
        //{
        //    try
        //    {
        //        var itemQuery = _dbContext.Items
        //            .Where(x => x.Id == itemId)
        //            .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
        //            .AsQueryable();

        //        var item = await AddSelectToItemQuery(itemQuery)
        //            .FirstOrDefaultAsync();

        //        if (item == null)
        //        {
        //            return new OperationDataResult<ItemModel>(false,
        //                _inventoryLocalizationService.GetString("ItemNotFound"));
        //        }

        //        return new OperationDataResult<ItemModel>(true, item);
        //    }
        //    catch (Exception e)
        //    {
        //        Trace.TraceError(e.Message);
        //        return new OperationDataResult<ItemModel>(false,
        //            _inventoryLocalizationService.GetString("ErrorWhileGetItems"));
        //    }

        //}

        public async Task <OperationResult> UpdateItem(ItemUpdateModel itemUpdateModel)
        {
            try
            {
                var item = await _dbContext.Items
                           .Where(x => x.Id == itemUpdateModel.Id)
                           .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                           .FirstOrDefaultAsync();

                if (item == null)
                {
                    return(new OperationResult(false,
                                               _inventoryLocalizationService.GetString("ItemNotFound")));
                }

                var option = _options.Value;

                if (option.InventoryFormId > 0 && option.FolderId > 0)
                {
                    var theCore = await _coreService.GetCore();

                    await using var sdkDbContext = theCore.DbContextHelper.GetDbContext();
                    var folder = sdkDbContext.Folders.Single(x => x.Id == option.FolderId);
                    // deploy cases
                    if (itemUpdateModel.Available && !item.Available)
                    {
                        foreach (var assignedSite in _dbContext.AssignedSites.Where(x =>
                                                                                    x.WorkflowState != Constants.WorkflowStates.Removed))
                        {
                            var siteLanguageId = await sdkDbContext.Sites
                                                 .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                                 .Where(x => x.MicrotingUid == assignedSite.SiteUid)
                                                 .Select(x => x.LanguageId)
                                                 .SingleAsync();

                            var language = await sdkDbContext.Languages.SingleAsync(x => x.Id == siteLanguageId);

                            var mainElement = await theCore.ReadeForm(option.InventoryFormId, language);

                            mainElement.CheckListFolderName = folder.Name;
                            mainElement.EndDate             = DateTime.UtcNow.AddYears(10);
                            mainElement.Repeated            = 0;
                            mainElement.PushMessageTitle    = mainElement.Label;

                            // ReSharper disable once PossibleInvalidOperationException
                            await theCore.CaseCreate(mainElement, "", assignedSite.SiteUid, (int)folder.MicrotingUid);
                        }
                    }

                    // retract cases
                    if (item.Available && !itemUpdateModel.Available)
                    {
                        foreach (var assignedSite in _dbContext.AssignedSites.Where(x =>
                                                                                    x.WorkflowState != Constants.WorkflowStates.Removed))
                        {
                            var siteLanguageId = await sdkDbContext.Sites
                                                 .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                                 .Where(x => x.MicrotingUid == assignedSite.SiteUid)
                                                 .Select(x => x.LanguageId)
                                                 .SingleAsync();

                            var language = await sdkDbContext.Languages.SingleAsync(x => x.Id == siteLanguageId);

                            var mainElement = await theCore.ReadeForm(option.InventoryFormId, language);

                            await theCore.CaseDelete(mainElement.MicrotingUId, assignedSite.SiteUid);
                        }
                    }
                }
                item.SN              = itemUpdateModel.SN;
                item.Available       = itemUpdateModel.Available;
                item.CustomerId      = itemUpdateModel.CustomerId;
                item.ExpirationDate  = itemUpdateModel.ExpirationDate;
                item.ItemTypeId      = itemUpdateModel.ItemTypeId;
                item.Location        = itemUpdateModel.Location;
                item.UpdatedByUserId = _userService.UserId;

                await item.Update(_dbContext);

                return(new OperationResult(true,
                                           _inventoryLocalizationService.GetString("ItemUpdatedSuccessfully")));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationResult(false,
                                           _inventoryLocalizationService.GetString("ErrorWhileUpdateItem")));
            }
        }
 public async Task <OperationResult> UpdateInventoryItemGroup([FromBody] ItemUpdateModel itemUpdateModel)
 {
     return(await _inventoryItemService.UpdateItem(itemUpdateModel));
 }
Beispiel #17
0
 public Task <Item> UpdateAsync(ItemUpdateModel item)
 {
     throw new System.NotImplementedException();
 }
Beispiel #18
0
 public IActionResult Put([FromBody] ItemUpdateModel model)
 {
     return Ok(_itemServieces.UpdateItemModel(model));
 }
Beispiel #19
0
        public async Task <Item> CreateAsync(ItemUpdateModel item)
        {
            await this.CategoryGetService.ValidateAsync(item);

            return(await this.ItemDataAccess.InsertAsync(item));
        }
Beispiel #20
0
 public async Task <Item> UpdateAsync(ItemUpdateModel item)
 {
     return(await this.ItemDataAccess.UpdateAsync(item));
 }