Exemple #1
0
        public async Task Edit_ExistingTodoButNotOwnedByTokenOwner_NotFound()
        {
            // Arrange
            var user       = MockApplicationUsers.Get(5);
            var todoToEdit = MockTodos.GetAll().FirstOrDefault(z => z.Owner.Id != user.Id);

            Assert.NotNull(todoToEdit);
            var token = await GetToken(user);

            var model = new EditTodoViewModel
            {
                Id          = todoToEdit.Id,
                Due         = DateTime.Now,
                Description = "Listen all of y'all it's a sabotage"
            };
            var body = JsonStringBuilder.EditTodoJsonBody(
                model.Description, model.Due.ToString(), model.Id.ToString());
            var content = new StringContent(body);
            var path    = Routes.TodoRoute;

            _endSystems.SetBearerToken(token);

            // Act
            var response = await _endSystems.Put(path, content);

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, response.Code);

            // Tear down
            _endSystems.Dispose();
        }
Exemple #2
0
        public async Task Edit_NonExistingTodo_NotFound()
        {
            // Arrange
            var user  = MockApplicationUsers.Get(5);
            var token = await GetToken(user);

            var model = new EditTodoViewModel
            {
                Id          = 0,
                Due         = DateTime.Now,
                Description = "Intergalactic, planetary, planetary, intergalactic"
            };
            var body = JsonStringBuilder.EditTodoJsonBody(
                model.Description, model.Due.ToString(), model.Id.ToString());
            var          content = new StringContent(body);
            const string path    = Routes.TodoRoute;

            _endSystems.SetBearerToken(token);

            // Act
            var response = await _endSystems.Put(path, content);

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, response.Code);

            // Tear down
            _endSystems.Dispose();
        }
 /// <summary>
 /// Edit this entity using a editing view model.
 /// </summary>
 /// <param name="model">A view model containing changed fields</param>
 public void Edit(EditTodoViewModel model)
 {
     if (model.Due != null)
     {
         Due = model.Due.Value;
     }
     Description = string.Copy(model.Description);
 }
Exemple #4
0
        public EditTodoPage(Todo todo)
        {
            var editTodoViewModel = new EditTodoViewModel();

            editTodoViewModel.SelectedTodo = todo;
            BindingContext = editTodoViewModel;
            InitializeComponent();
        }
        public Todo UpdateTodoFromModel(EditTodoViewModel model, Todo todo)
        {
            todo.Title    = model.Title.Trim();
            todo.Summary  = model.Summary.Trim();
            todo.Hashtag  = ValidateHashtag(model.Hashtag);
            todo.Deadline = model.Deadline;
            todo.Priority = model.Priority;

            return(todo);
        }
        public EditTodoViewModel GetEditViewModel(Todo todo)
        {
            var model = new EditTodoViewModel
            {
                Id          = todo.Id,
                Title       = todo.Title,
                Hashtag     = todo.Hashtag,
                Summary     = todo.Summary,
                Deadline    = todo.Deadline,
                Priority    = todo.Priority,
                IsCompleted = todo.IsCompleted
            };

            return(model);
        }
        public async Task <IActionResult> Edit(EditTodoViewModel model)
        {
            if (ModelState.IsValid)
            {
                var toUpdate = await todoRepository.GetByIdAsync(model.Id);

                var todo = viewModelService.UpdateTodoFromModel(model, toUpdate);

                await todoRepository.UpdateAsync(todo);
                await SendUserDateOnTodosUpdateAsync();

                return(RedirectToAction("index", "todos"));
            }
            return(View());
        }
Exemple #8
0
        public async Task EditTodoAsync_NonExistingTodo_TodoNotFoundException()
        {
            // Arrange
            var userId = MockApplicationUsers.Get(0).Id;
            var model  = new EditTodoViewModel
            {
                Id          = 0,
                Due         = DateTime.Now,
                Description = "Take to the sewers, in a clown costume"
            };

            // Act
            // Assert
            await Assert.ThrowsAsync <TodoNotFoundException>(() => _service.EditTodoAsync(model, userId));
        }
        public void EditTodoViewModel_Getter_Matches()
        {
            // Arrange
            // Act
            var model = new EditTodoViewModel
            {
                Id          = 5199,
                Description = "Walk like an Egyptian",
                Due         = new DateTime(2031, 8, 11, 18, 0, 0)
            };

            // Assert
            Assert.Equal(5199, model.Id);
            Assert.Equal("Walk like an Egyptian", model.Description);
            Assert.Equal(new DateTime(2031, 8, 11, 18, 0, 0), model.Due);
        }
Exemple #10
0
        public async Task EditTodoAsync_NonExistingUser_TodoNotFoundException()
        {
            // Arrange
            const string userId = "c54a85fa-ca7c-49d7-b830-6b07ea49cfa8";
            var          todo   = MockTodos.Get(3);
            var          model  = new EditTodoViewModel
            {
                Id          = todo.Id,
                Due         = todo.Due,
                Description = todo.Description
            };

            // Act
            // Assert
            await Assert.ThrowsAsync <TodoNotFoundException>(() => _service.EditTodoAsync(model, userId));
        }
Exemple #11
0
        public async Task Edit_ExistingTodoOwnedByTokenOwner_Ok()
        {
            // Arrange
            var user       = MockApplicationUsers.Get(2);
            var todoToEdit = MockTodos.GetAll().LastOrDefault(z => z.Owner.Id == user.Id);

            Assert.NotNull(todoToEdit);
            var token = await GetToken(user);

            var model = new EditTodoViewModel
            {
                Id          = todoToEdit.Id,
                Due         = new DateTime(2014, 12, 12, 10, 11, 12),
                Description = "It's like a jungle sometimes - It makes me wonder how I keep from goin' under"
            };
            var body = JsonStringBuilder.EditTodoJsonBody(
                model.Description, model.Due.ToString(), model.Id.ToString());
            var content = new StringContent(body);

            _endSystems.SetBearerToken(token);
            var getPath = $"{Routes.TodoRoute}/{model.Id}";
            var putPath = Routes.TodoRoute;

            // Act
            var getResponse1 = await _endSystems.Get(getPath);

            var editResponse = await _endSystems.Put(putPath, content);

            var getResponse2 = await _endSystems.Get(getPath);

            var dtoBefore = JsonStringSerializer.GetTodoDto(getResponse1.Body);
            var dtoAfter  = JsonStringSerializer.GetTodoDto(getResponse2.Body);

            // Assert
            Assert.Equal(HttpStatusCode.OK, getResponse1.Code);
            Assert.Equal(HttpStatusCode.OK, editResponse.Code);
            Assert.Equal(HttpStatusCode.OK, getResponse2.Code);
            Assert.Equal(todoToEdit.Id, dtoBefore.Id);
            Assert.Equal(todoToEdit.Due, dtoBefore.Due);
            Assert.Equal(todoToEdit.Description, dtoBefore.Description);
            Assert.Equal(model.Id, dtoAfter.Id);
            Assert.Equal(model.Due, dtoAfter.Due);
            Assert.Equal(model.Description, dtoAfter.Description);

            // Tear down
            _endSystems.Dispose();
        }
        public void EditTodoViewModel_IdMissing_Error()
        {
            // Arrange
            var model = new EditTodoViewModel
            {
                Description = "Build a ladder to heaven",
                Due         = DateTime.Now
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoIdRequired, errorMessage);
        }
        public void EditTodoViewModel_DueMissing_Error()
        {
            // Arrange
            var model = new EditTodoViewModel
            {
                Description = "Give an angry speech before being killed by a shark",
                Id          = 5555
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoDueRequired, errorMessage);
        }
        public void EditTodoViewModel_DescriptionTooLong_Error()
        {
            // Arrange
            var len   = TodoLimits.DescriptionMaxLength + 1;
            var model = new EditTodoViewModel
            {
                Description = new StringBuilder(len).Insert(0, "A", len).ToString(),
                Due         = DateTime.Now,
                Id          = 314159
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoDescriptionMaxLength, errorMessage);
        }
Exemple #15
0
        public async Task <IActionResult> EditTodo([FromBody] EditTodoViewModel changedTodo)
        {
            if (changedTodo == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                await _todoService.EditTodoAsync(changedTodo, GetUserId());

                return(Ok());
            }
            catch (TodoNotFoundException)
            {
                return(NotFound());
            }
        }
Exemple #16
0
        /// <inheritdoc />
        /// <exception cref="TodoNotFoundException">When todo is not found</exception>
        public async Task EditTodoAsync(EditTodoViewModel model, string userId)
        {
            var todo = await _db.Todo.SingleOrDefaultAsync(t => t.Id == model.Id && t.Owner.Id == userId);

            if (todo == null)
            {
                throw new TodoNotFoundException();
            }
            var oldDate = todo.Due;

            todo.Edit(model);
            await _db.SaveChangesAsync();

            // Set cache for single and clear related lists
            _cache.Set(CacheConstants.GetSingleTodoCacheKey(todo.Id, userId),
                       todo, CacheConstants.GetDefaultCacheOptions());
            _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId));
            _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, oldDate));
            if (oldDate.Date != todo.Due.Date)
            {
                _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, todo.Due));
            }
        }
Exemple #17
0
        public async Task EditTodoAsync_ValidUserAndTodo_Edited()
        {
            // Arrange
            var user = MockApplicationUsers.Get(4);
            var todo = _ctx.Todo.FirstOrDefault(z => z.Owner.Id == user.Id);

            Assert.NotNull(todo);
            var model = new EditTodoViewModel
            {
                Id          = todo.Id,
                Description = "Catch Mr. X",
                Due         = new DateTime(2020, 1, 1, 0, 0, 0)
            };

            // Act
            await _service.EditTodoAsync(model, user.Id);

            var changedTodo = _ctx.Todo.SingleOrDefault(z => z.Id == todo.Id);

            // Assert
            Assert.NotNull(changedTodo);
            Assert.Equal(new DateTime(2020, 1, 1, 0, 0, 0), changedTodo.Due);
            Assert.Equal("Catch Mr. X", changedTodo.Description);
        }
Exemple #18
0
 public EditTodoPage(Guid id = default(Guid))
 {
     InitializeComponent();
     BindingContext = new EditTodoViewModel(id);
 }
        /// <inheritdoc />
        public async Task EditTodoAsync(EditTodoViewModel model, string userId)
        {
            await Task.Run(() => { });

            MEditTodoAsync(model, userId);
        }