Пример #1
0
        public async void DeleteAsync_GivenCategoryExistsAndInProjects_ReturnsSuccess()
        {
            var categoryToDelete = new Category
            {
                Id   = 1,
                Name = "Category"
            };

            var creatorUser = new User()
            {
                Id = 1, Firstname = "First", Surname = "Sur", AzureUId = "Azure", Mail = "*****@*****.**"
            };

            var projects = new Project[]
            {
                new Project {
                    Title = "Project1", Category = categoryToDelete, CreatedDate = DateTime.Now, Description = "abcd", CreatorId = creatorUser.Id
                },
                new Project {
                    Title = "Project2", Category = categoryToDelete, CreatedDate = DateTime.Now, Description = "abcd", CreatorId = creatorUser.Id
                }
            };

            categoryRepository = new CategoryRepository(setupContextForIntegrationTests());

            context.Categories.Add(categoryToDelete);
            context.Users.Add(creatorUser);
            context.Projects.AddRange(projects);
            context.SaveChanges();

            //Sanity Check
            Assert.NotNull(context.Categories.Find(categoryToDelete.Id));
            Assert.Equal(2, (await context.Projects.ToArrayAsync()).Length);

            using (var logic = new CategoryLogic(categoryRepository, projectRepositoryMock.Object))
            {
                var response = await logic.DeleteAsync(categoryToDelete.Id);

                Assert.Equal(ResponseLogic.SUCCESS, response);
                foreach (var project in await context.Projects.ToArrayAsync())
                {
                    Assert.Null(project.Category);
                }
            }
        }
Пример #2
0
        public async void DeleteAsync_GivenDatabaseError_ReturnsERROR_DELETING()
        {
            var categoryToDelete = new CategoryDTO
            {
                Id   = 1,
                Name = "Category"
            };

            categoryRepositoryMock.Setup(c => c.FindAsync(categoryToDelete.Id)).ReturnsAsync(categoryToDelete);
            categoryRepositoryMock.Setup(c => c.DeleteAsync(categoryToDelete.Id)).ReturnsAsync(false);

            using (var logic = new CategoryLogic(categoryRepositoryMock.Object, projectRepositoryMock.Object))
            {
                var response = await logic.DeleteAsync(categoryToDelete.Id);

                Assert.Equal(ResponseLogic.ERROR_DELETING, response);
                categoryRepositoryMock.Verify(c => c.FindAsync(categoryToDelete.Id));
                categoryRepositoryMock.Verify(c => c.DeleteAsync(categoryToDelete.Id));
            }
        }
Пример #3
0
        public async void DeleteAsync_GivenCategoryExistsAndInNoProjects_ReturnsSuccess()
        {
            var categoryToDelete = new Category
            {
                Id   = 1,
                Name = "Category"
            };

            categoryRepository = new CategoryRepository(setupContextForIntegrationTests());

            context.Categories.Add(categoryToDelete);
            context.SaveChanges();

            //Sanity Check
            Assert.NotNull(context.Categories.Find(categoryToDelete.Id));
            Assert.Empty(await context.Projects.ToArrayAsync());

            using (var logic = new CategoryLogic(categoryRepository, projectRepositoryMock.Object))
            {
                var response = await logic.DeleteAsync(categoryToDelete.Id);

                Assert.Equal(ResponseLogic.SUCCESS, response);
            }
        }
Пример #4
0
        private async void OnSave(object obj)
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                // Thuc hien cong viec tai day
                if (IsEditing) //neu dang o che do chinh sua
                {
                    var categoryLogic = new CategoryLogic(_dbContext);
                    // Kiem tra them xoa sua
                    foreach (var category in ListCategoryBindProp)
                    {
                        switch (category.Status)
                        {
                        case Status.New:
                            var newCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName
                            };
                            await categoryLogic.CreateAsync(newCategory, false);

                            break;

                        case Status.Normal:
                            break;

                        case Status.Modified:
                            var modCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName
                            };
                            await categoryLogic.UpdateAsync(modCategory, false);

                            break;

                        case Status.Deleted:
                            break;

                        default:
                            break;
                        }
                        MessagingCenter.Send(category, Messages.CATEGORY_MESSAGE);
                    }

                    foreach (var cate in _listDeleted)
                    {
                        cate.Status = Status.Deleted;
                        await categoryLogic.DeleteAsync(cate.Id, false);

                        MessagingCenter.Send(cate, Messages.CATEGORY_MESSAGE);
                    }
                    //neu co nhap category moi thi tao roi quay ve
                    if (!string.IsNullOrWhiteSpace(NewCategoryNameBindProp))
                    {
                        var newCate = new Category
                        {
                            Id           = Guid.NewGuid().ToString(),
                            CategoryName = NewCategoryNameBindProp,
                        };
                        var newVsCate = new VisualCategoryModel
                        {
                            Id           = newCate.Id,
                            CategoryName = newCate.CategoryName,
                            Status       = Status.New
                        };
                        await categoryLogic.CreateAsync(newCate, false);

                        ListCategoryBindProp.Add(newVsCate);
                        MessagingCenter.Send(newCate, Messages.CATEGORY_MESSAGE);
                    }

                    await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                    await NavigationService.GoBackAsync();
                }
                else // neu dang o che do binh thuong
                {
                    var categoryLogic = new CategoryLogic(_dbContext);
                    // Kiem tra them moi
                    foreach (var category in ListCategoryBindProp)
                    {
                        switch (category.Status)
                        {
                        case Status.New:
                            var newCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName,
                            };
                            await categoryLogic.CreateAsync(newCategory, false);

                            MessagingCenter.Send(category, Messages.CATEGORY_MESSAGE);
                            category.Status = Status.Normal;
                            break;

                        case Status.Normal:
                            break;

                        case Status.Modified:
                            break;

                        case Status.Deleted:
                            break;
                        }
                    }

                    // neu khong nhap gi
                    if (string.IsNullOrWhiteSpace(NewCategoryNameBindProp))
                    {
                        //neu co chon thi tra ve ten category
                        if (SelectedCategoryBindProp != null)
                        {
                            //truyen ten category ve trang tao item
                            var param = new NavigationParameters();
                            param.Add(Keys.CATEGORY, SelectedCategoryBindProp);

                            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                            await NavigationService.GoBackAsync(param);
                        }
                        else
                        {
                            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                            return;
                        }
                    }
                    else // tao moi
                    {
                        var newCate = new Category
                        {
                            Id           = Guid.NewGuid().ToString(),
                            CategoryName = NewCategoryNameBindProp,
                        };
                        var newVsCate = new VisualCategoryModel
                        {
                            Id           = newCate.Id,
                            CategoryName = newCate.CategoryName,
                            Status       = Status.New
                        };
                        await categoryLogic.CreateAsync(newCate, false);

                        ListCategoryBindProp.Add(newVsCate);
                        MessagingCenter.Send(newCate, Messages.CATEGORY_MESSAGE);

                        await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                        //truyen ten category ve trang tao item
                        var param = new NavigationParameters();
                        param.Add(Keys.CATEGORY, newCate);

                        await NavigationService.GoBackAsync(param);
                    }
                }
            }
            catch (Exception e)
            {
                await ShowError(e);
            }
            finally
            {
                IsBusy = false;
            }
        }