コード例 #1
0
        public SuccessResponse <LabelDto> CreateLabel([FromBody] CreateLabelDto createLabelDto)
        {
            var label    = _mapper.Map <Label>(createLabelDto);
            var response = _labelService.CreateLabel(label);

            return(new SuccessResponse <LabelDto>(_mapper.Map <LabelDto>(response)));
        }
コード例 #2
0
        /// <summary>
        /// Adds the specified label dto.
        /// </summary>
        /// <param name="labelDto">The label dto.</param>
        /// <returns></returns>
        public async Task <LabelDto> Add(CreateLabelDto labelDto)
        {
            Label label = _mapper.Map <Label>(labelDto);

            label = _labelRepository.Add(label);
            await _labelRepository.Save();

            return(_mapper.Map <LabelDto>(label));
        }
コード例 #3
0
        /// <summary>
        /// adds label in Label table
        /// </summary>
        /// <param name="createLabelDto"></param>
        /// <returns> added label record Dto. </returns>
        public async Task <LabelDto> AddLabel(CreateLabelDto createLabelDto)
        {
            LabelDbModel labelDbDto = _mapper.Map <LabelDbModel>(createLabelDto);

            labelDbDto.CreatedBy = createLabelDto.CreatedBy;
            _toDoDbContext.Labels.Add(labelDbDto);
            await _toDoDbContext.SaveChangesAsync();

            return(_mapper.Map <LabelDto>(labelDbDto));
        }
コード例 #4
0
        /// <summary>
        /// Adds Label record
        /// </summary>
        /// <param name="createLabelDto"></param>
        /// <returns> added ToDoList record. </returns>
        public async Task <LabelDto> AddLabel(CreateLabelDto createLabelDto)
        {
            if (createLabelDto != null)
            {
                createLabelDto.CreatedBy = _userId;
            }
            LabelDto addedItem = await _labelDbOps.AddLabel(createLabelDto);

            return(addedItem);
        }
コード例 #5
0
        public void CreateLabel_ShouldCallCreateLabel()
        {
            // Arrange
            var input = new CreateLabelDto();

            // Act
            controller.AssignLabel(input);

            // Assert
            todoItemLogic.Verify(u => u.CreateLabel(1, It.Is <CreateLabelDto>(c => c == input)));
        }
コード例 #6
0
        public void CreateLabel_ShouldCallCreateLabel()
        {
            // Arrange
            var input = new CreateLabelDto();

            // Act
            logic.CreateLabel(1, input);

            // Assert
            todoItemRepository.Verify(t => t.CreateLabel(1, input));
        }
コード例 #7
0
        public void CreateLabel_ShouldMapResultToDto()
        {
            // Arrange
            var input = new CreateLabelDto();

            // Act
            logic.CreateLabel(1, input);

            // Assert
            mapper.Verify(t => t.Map <LabelDto>(It.IsAny <Label>()));
        }
コード例 #8
0
        public void CreateLabel_ShouldReturnBadRequestWhenCreateLabelReturnsNull()
        {
            // Arrange
            var input = new CreateLabelDto();

            // Act
            var result = controller.AssignLabel(input);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
            var response = (result as BadRequestObjectResult).Value as ErrorResponse;

            Assert.Equal("User or item not found in the database.", response.Message);
        }
コード例 #9
0
        public void CreateLabel_ShouldReturnCreatedLabel()
        {
            // Arrange
            var input = new CreateLabelDto();
            var model = new LabelDto();

            todoItemLogic.Setup(u => u.CreateLabel(1, It.Is <CreateLabelDto>(c => c == input))).Returns(model);

            // Act
            var result = controller.AssignLabel(input);

            // Assert
            Assert.IsType <CreatedAtActionResult>(result);
        }
コード例 #10
0
        public void CreateLabel_ShouldReturnNullIfItemIsNotFound()
        {
            // Arrange
            var dbContext  = SetupDatabase(nameof(CreateLabel_ShouldReturnNullIfItemIsNotFound));
            var repository = new TodoItemRepository(dbContext);
            var input      = new CreateLabelDto
            {
                ParentId = 20,
                Label    = "Cheese"
            };

            // Act
            var result = repository.CreateLabel(1, input);

            // Assert
            Assert.Null(result);
        }
コード例 #11
0
        /// <summary>
        /// Creates a label for todo item.
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="createLabelDto">label for todo item to be created</param>
        /// <returns>Label dto</returns>
        public Label CreateLabel(int userId, CreateLabelDto createLabelDto)
        {
            var user = dbContext.Users.FirstOrDefault(u => u.UserId == userId);

            if (user == null)
            {
                return(null);
            }

            var existingItem = dbContext.TodoItems
                               .Include(l => l.Labels)
                               .FirstOrDefault(u => u.Id == userId && u.Id == createLabelDto.ParentId);

            if (existingItem == null)
            {
                return(null);
            }

            // if label already exists return that label instead of creating new
            var existingLabel = existingItem.Labels.FirstOrDefault(l => l.Name == createLabelDto.Label);

            if (existingLabel != null)
            {
                return(existingLabel);
            }

            var label = new Label
            {
                Name         = createLabelDto.Label,
                user         = user,
                LastModified = DateTime.Now
            };

            if (existingItem.Labels == null)
            {
                existingItem.Labels = new List <Label>();
            }

            existingItem.Labels.Add(label);

            dbContext.SaveChanges();

            return(label);
        }
コード例 #12
0
        public async Task <IActionResult> CreateLabel(CreateLabelModel createLabelModel, ApiVersion version)
        {
            long userId = long.Parse(HttpContext.Items["UserId"].ToString());

            if (createLabelModel == null || string.IsNullOrWhiteSpace(createLabelModel.Description))
            {
                return(BadRequest(new ApiResponse <string>
                {
                    IsSuccess = false,
                    Result = "Not Updated.",
                    Message = "Please enter correct values. Description should not be empty."
                }));
            }
            createLabelModel.CreatedBy = userId;
            CreateLabelDto createLabelDto = _mapper.Map <CreateLabelDto>(createLabelModel);
            LabelDto       createdLabel   = await _labelContract.AddLabel(createLabelDto);

            return(CreatedAtAction(nameof(GetLabelById), new { createdLabel.LabelId, version = $"{version}" }, createdLabel));
        }
コード例 #13
0
        public async Task <IActionResult> Add(CreateLabelDto labelDto, ApiVersion version)
        {
            int userId = int.Parse(HttpContext.Items["UserId"].ToString());

            if (labelDto == null || string.IsNullOrWhiteSpace(labelDto.Description))
            {
                return(BadRequest(new ResponseModel <string>
                {
                    IsSuccess = false,
                    Result = "Not Updated.",
                    Message = "Invalid request, Mandatory fields not provided in request."
                }));
            }
            labelDto.CreatedBy = userId;
            labelDto.UserId    = userId;
            LabelDto createdLabel = await _labelService.Add(labelDto);

            return(CreatedAtAction(nameof(GetById), new { id = createdLabel.LabelId, version = $"{version}" }, createdLabel));
        }
コード例 #14
0
        public void CreateLabel_ShouldCreateLabel()
        {
            // Arrange
            var dbContext  = SetupDatabase(nameof(CreateLabel_ShouldCreateLabel));
            var repository = new TodoItemRepository(dbContext);
            var input      = new CreateLabelDto
            {
                ParentId = 1,
                Label    = "Test label"
            };

            // Act
            var result = repository.CreateLabel(1, input);

            // Assert
            Assert.NotNull(result);
            var item = dbContext.Labels.FirstOrDefault(t => t.Name == "Test label");

            Assert.NotNull(item);
        }
コード例 #15
0
        public IActionResult AssignLabel(CreateLabelDto input)
        {
            var userId = int.Parse(httpContextAccessor.HttpContext.User.FindFirst(Constants.UserIdClaim)?.Value);
            var result = todoItemLogic.CreateLabel(userId, input);

            if (result == null)
            {
                return(BadRequest(new ErrorResponse
                {
                    Status = false,
                    Message = "User or item not found in the database."
                }));
            }
            else
            {
                return(CreatedAtAction(nameof(GetItemById), new { result.Id }, new Response <LabelDto>
                {
                    Status = true,
                    Model = result
                }));
            }
        }
コード例 #16
0
 /// <summary>
 /// adds label record to label table.
 /// </summary>
 /// <param name="createLabelDto"></param>
 /// <returns> Added Label record. </returns>
 public async Task <LabelDto> AddLabel(CreateLabelDto createLabelDto)
 {
     return(await _labelDbOps.AddLabel(createLabelDto));
 }
コード例 #17
0
 /// <summary>
 /// Adds the specified label dto.
 /// </summary>
 /// <param name="labelDto">The label dto.</param>
 /// <returns></returns>
 public async Task <LabelDto> Add(CreateLabelDto labelDto)
 {
     return(await _labelRepository.Add(labelDto));
 }
コード例 #18
0
        public Label AssignLabelToItem(CreateLabelDto createLabelDto)
        {
            var userId = CheckAuthentication();

            return(todoItemRepository.CreateLabel(userId, createLabelDto));
        }
コード例 #19
0
        /// <summary>
        /// Creates a label for todo item.
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="createLabelDto">label for todo List to be created</param>
        /// <returns>Label dto</returns>
        public LabelDto CreateLabel(int userId, CreateLabelDto createLabelDto)
        {
            var dbLabel = todoItemRepository.CreateLabel(userId, createLabelDto);

            return(mapper.Map <LabelDto>(dbLabel));
        }