public async Task SaveTaskAsync_ValidWorkTaskToAdd_SuccessResponseReturnedAndSavePerformed()
        {
            IWorkTask validWorkTask = A.Fake <IWorkTask>();

            validWorkTask.GroupName   = "groupName";
            validWorkTask.Description = "validDescription";

            ITasksGroup tasksGroup = mTasksGroupFactory.CreateGroup("bla");

            tasksGroup.SetGroupName(validWorkTask.GroupName);

            IDbRepository <ITasksGroup> dbRepository = A.Fake <IDbRepository <ITasksGroup> >();

            A.CallTo(() => dbRepository.ListAsync()).Returns(new List <ITasksGroup>()
            {
                tasksGroup
            });
            TasksGroupService tasksGroupService =
                new TasksGroupService(dbRepository, mTasksGroupFactory, NullLogger <TasksGroupService> .Instance);

            IResponse <IWorkTask> response =
                await tasksGroupService.SaveTaskAsync(validWorkTask.GroupName, validWorkTask.Description)
                .ConfigureAwait(false);

            Assert.True(response.IsSuccess);
            Assert.Equal(tasksGroup.GetAllTasks().First(), response.ResponseObject);
            A.CallTo(() => dbRepository.UpdateAsync(A <ITasksGroup> .Ignored)).MustHaveHappenedOnceExactly();
        }
        public async Task SaveTaskAsync_DescriptionExistsInTheSameGroup_SaveNotPerformed()
        {
            IWorkTask workTask = A.Fake <IWorkTask>();

            workTask.GroupName   = "groupName";
            workTask.Description = "description";

            IWorkTask workTaskWithSameDescription = A.Fake <IWorkTask>();

            workTask.GroupName   = workTask.GroupName;
            workTask.Description = workTask.Description;

            ITasksGroup tasksGroup = A.Fake <ITasksGroup>();

            tasksGroup.SetGroupName(workTask.GroupName);
            A.CallTo(() => tasksGroup.GetAllTasks()).Returns(new List <IWorkTask> {
                workTask
            });

            IDbRepository <ITasksGroup> dbRepository = A.Fake <IDbRepository <ITasksGroup> >();

            A.CallTo(() => dbRepository.ListAsync()).Returns(new List <ITasksGroup>()
            {
                tasksGroup
            });

            TasksGroupService tasksGroupService =
                new TasksGroupService(dbRepository, mTasksGroupFactory, NullLogger <TasksGroupService> .Instance);

            await tasksGroupService.SaveTaskAsync(workTask.GroupName, workTask.Description).ConfigureAwait(false);

            A.CallTo(() => dbRepository.UpdateAsync(A <ITasksGroup> .Ignored)).MustNotHaveHappened();
        }
        public async Task SaveTaskAsync_DescriptionExistsInTheSameGroup_FailResponseReturned()
        {
            IWorkTask workTask = A.Fake <IWorkTask>();

            workTask.GroupName   = "groupName";
            workTask.Description = "description";

            IWorkTask workTaskWithSameDescription = A.Fake <IWorkTask>();

            workTask.GroupName   = workTask.GroupName;
            workTask.Description = workTask.Description;

            ITasksGroup tasksGroup = A.Fake <ITasksGroup>();

            tasksGroup.SetGroupName(workTask.GroupName);
            A.CallTo(() => tasksGroup.GetAllTasks()).Returns(new List <IWorkTask> {
                workTask
            });

            IDbRepository <ITasksGroup> dbRepository = A.Fake <IDbRepository <ITasksGroup> >();

            A.CallTo(() => dbRepository.ListAsync()).Returns(new List <ITasksGroup>()
            {
                tasksGroup
            });

            TasksGroupService tasksGroupService =
                new TasksGroupService(dbRepository, mTasksGroupFactory, NullLogger <TasksGroupService> .Instance);

            IResponse <IWorkTask> response =
                await tasksGroupService.SaveTaskAsync(workTask.GroupName, workTask.Description).ConfigureAwait(false);

            Assert.False(response.IsSuccess);
            Assert.Null(response.ResponseObject);
        }
        public async Task SaveTaskAsync_InvalidTaskName_FailResponseReturnedAndSaveNotPerformed()
        {
            const string groupName = "groupName";

            IWorkTask workTask = A.Fake <IWorkTask>();

            workTask.GroupName   = groupName;
            workTask.Description = mInvalidTaskName;
            OperationResult <IWorkTask> createTaskResult = new OperationResult <IWorkTask>(true, workTask);

            ITasksGroup tasksGroup = A.Fake <ITasksGroup>();

            tasksGroup.SetGroupName(groupName);

            A.CallTo(() => tasksGroup.CreateTask(A <string> .Ignored, A <string> .Ignored)).Returns(createTaskResult);

            IDbRepository <ITasksGroup> dbRepository = A.Fake <IDbRepository <ITasksGroup> >();

            A.CallTo(() => dbRepository.ListAsync()).Returns(new List <ITasksGroup> {
                tasksGroup
            });

            TasksGroupService tasksGroupService =
                new TasksGroupService(dbRepository, mTasksGroupFactory, NullLogger <TasksGroupService> .Instance);

            IResponse <IWorkTask> response =
                await tasksGroupService.SaveTaskAsync(workTask.GroupName, workTask.Description).ConfigureAwait(false);

            Assert.False(response.IsSuccess);
            Assert.Null(response.ResponseObject);
            A.CallTo(() => dbRepository.AddAsync(A <ITasksGroup> .Ignored)).MustNotHaveHappened();
        }
        public async Task UpdateAsync_GroupExists_GroupUpdated()
        {
            AppDbContext database = new AppDbContext(
                Options.Create(new DatabaseConfigurtaion()),
                mObjectSerializer,
                mIDProducer,
                NullLogger <AppDbContext> .Instance);

            TasksGroupRepository tasksGroupRepository =
                new TasksGroupRepository(database, NullLogger <TasksGroupRepository> .Instance);

            ITasksGroup tasksGroup = mTasksGroupFactory.CreateGroup("group1");

            database.Entities.Add(tasksGroup);

            Assert.Single(await tasksGroupRepository.ListAsync().ConfigureAwait(false));

            ITasksGroup tasksGroupWithUpdate = await tasksGroupRepository.FindAsync(tasksGroup.ID).ConfigureAwait(false);

            const string newGroupName = "group1_changed";

            tasksGroupWithUpdate.SetGroupName(newGroupName);

            await tasksGroupRepository.UpdateAsync(tasksGroupWithUpdate).ConfigureAwait(false);

            Assert.Single(await tasksGroupRepository.ListAsync().ConfigureAwait(false));

            ITasksGroup updatedTasksGroup = await tasksGroupRepository.FindAsync(tasksGroup.ID).ConfigureAwait(false);

            Assert.Equal(newGroupName, updatedTasksGroup.Name);
        }
        public async Task <IResponse <ITasksGroup> > UpdateGroupAsync(string id, string newGroupName)
        {
            if (!mTasksGroupNameValidator.IsNameValid(newGroupName))
            {
                return(new FailResponse <ITasksGroup>($"Group name '{newGroupName}' is invalid"));
            }

            if (await IsTasksGroupNameAlreadyExist(newGroupName).ConfigureAwait(false))
            {
                return(new FailResponse <ITasksGroup>($"Group name '{newGroupName}' is already exists"));
            }

            ITasksGroup groupToUpdate = await mTasksGroupRepository.FindAsync(id).ConfigureAwait(false);

            if (groupToUpdate == null)
            {
                return(new FailResponse <ITasksGroup>("Group not found"));
            }

            try
            {
                OperationResult setGroupNameResult = groupToUpdate.SetGroupName(newGroupName);

                if (!setGroupNameResult.Success)
                {
                    return(new FailResponse <ITasksGroup>(setGroupNameResult.Reason));
                }

                await UpdateGroupNamesForAllChildren(groupToUpdate).ConfigureAwait(false);

                await mTasksGroupRepository.UpdateAsync(groupToUpdate).ConfigureAwait(false);

                return(new SuccessResponse <ITasksGroup>(groupToUpdate));
            }
            catch (Exception ex)
            {
                return(new FailResponse <ITasksGroup>($"An error occurred when updating tasks group id {id}: {ex.Message}"));
            }
        }