예제 #1
0
        public IHttpActionResult Post([FromBody] WorkCreateDto work)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                Work newWork = new Work
                {
                    ComposerID  = work.ComposerId,
                    Title       = work.Title,
                    EraID       = work.EraId,
                    Description = work.Description,
                    Year        = work.Year
                };

                workService.Add(newWork);
                workService.Save();

                var dtoWork = new WorkDto(newWork);
                return(Ok(dtoWork));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
예제 #2
0
        public async void CreateWorkTest(
            Status expectedStatus,
            DateTime startTime,
            DateTime endTime,
            bool withProject   = false,
            bool withDeveloper = false
            )
        {
            var developer = EntitiesFactory.NewDeveloper().Get();
            var project   = EntitiesFactory.NewProject(developerIds: new[] { developer.Id }).Save();

            var workDto = new WorkCreateDto
            {
                DeveloperId = developer.Id,
                ProjectId   = project.Id,
                StartTime   = startTime,
                EndTime     = endTime,
                Comment     = RandomHelper.RandomString(180),
                Hours       = 10
            };
            var worksPersisted = new List <Work>();

            _projectRepository.Setup(p => p.ExistAsync(project.Id)).ReturnsAsync(withProject);
            _projectRepository.Setup(p => p.GetDeveloperProjectIdAsync(project.Id, developer.Id))
            .ReturnsAsync(project.DeveloperProjects.Single().Id);
            _projectRepository.Setup(p => p.ExistDeveloperVinculatedAsync(project.Id, developer.Id))
            .ReturnsAsync(expectedStatus != Status.NotAllowed);
            _developerRepository.Setup(p => p.ExistAsync(developer.Id)).ReturnsAsync(withDeveloper);
            _workRepository.Setup(d => d.CreateAsync(Capture.In(worksPersisted)));
            _mockyRepository.Setup(m => m.SendNotificationAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new Result <bool>(expectedStatus != Status.Error));

            var service = new WorkService(
                _workRepository.Object,
                _developerRepository.Object,
                _projectRepository.Object,
                _mockyRepository.Object
                );
            var result = await service.CreateWorkAsync(workDto);

            Assert.Equal(expectedStatus, result.Status);
            if (expectedStatus == Status.Success)
            {
                _workRepository.Verify(d => d.CreateAsync(It.IsAny <Work>()), Times.Once);
                _mockyRepository.Verify(d => d.SendNotificationAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
                var work = worksPersisted.Single();
                Assert.Equal(workDto.Comment, work.Comment);
                Assert.Equal(workDto.StartTime, work.StartTime);
                Assert.Equal(workDto.EndTime, work.EndTime);
                Assert.Equal(workDto.Hours, work.Hours);
            }
        }
예제 #3
0
        public async Task <Result> CreateWorkAsync(WorkCreateDto workDto)
        {
            var resultValidation = ValidateRanteDateTime(workDto.StartTime, workDto.EndTime);

            if (!resultValidation.Success)
            {
                return(resultValidation);
            }
            var existProject = await _projectRepository.ExistAsync(workDto.ProjectId);

            if (!existProject)
            {
                return(new Result(Status.NotFund, $"Project with {nameof(workDto.ProjectId)} does not exist"));
            }
            var existDeveloper = await _developerRepository.ExistAsync(workDto.DeveloperId);

            if (!existDeveloper)
            {
                return(new Result(Status.NotFund, $"Developer with {nameof(workDto.DeveloperId)} does not exist"));
            }
            var developerVinculatedProject = await _projectRepository.ExistDeveloperVinculatedAsync(workDto.ProjectId, workDto.DeveloperId);

            if (!developerVinculatedProject)
            {
                return(new Result(Status.NotAllowed, $"Developer is not vinculated in Project"));
            }

            var developerProjectId = await _projectRepository.GetDeveloperProjectIdAsync(workDto.ProjectId, workDto.DeveloperId);

            var work = new Work(
                id: workDto.Id,
                developerProjectId: developerProjectId,
                startTime: workDto.StartTime,
                endTime: workDto.EndTime,
                comment: workDto.Comment,
                hours: workDto.Hours
                );

            await _workRepository.CreateAsync(work);

            var result = await _mockyService.SendNotificationAsync("Lançamento de horas", "Um novo lançamento de horas foi realizado");

            if (!result.Success || !result.Data)
            {
                return(new Result(Status.Error, result.ErrorMessages));
            }
            return(new Result());
        }
예제 #4
0
        public async Task <Result> CreateWorkProjectAsync([FromBody] WorkClientDto workDto, [FromRoute] Guid id)
        {
            var workCreateDto = new WorkCreateDto(workDto, id, _context.Id);

            return(GetResult(await _workService.CreateWorkAsync(workCreateDto)));
        }