public ProjectConfirmationDto AddProject(ProjectCreationDto project)
        {
            var addedProject = ProjectRepository.AddProject(Mapper.Map <Project>(project));

            ProjectRepository.SaveChanges();
            return(Mapper.Map <ProjectConfirmationDto>(addedProject));
        }
        public ActionResult <ProjectConfirmationDto> AddProject(ProjectCreationDto project)
        {
            var    createdProject = ProjectServices.AddProject(project);
            string location       = LinkGenerator.GetPathByAction("GetProjectByID", "Project", new { createdProject.Id });

            return(Created(location, createdProject));
        }
        public ActionResult Add(ProjectCreationDto projectCreationDto)
        {
            var result = _projectGeneralService.Add(projectCreationDto);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
        public IResult Add(ProjectCreationDto projectCreationDto)
        {
            var project = _mapper.Map <ProjectCreationDto, Project>(projectCreationDto);

            project.ActiveWorkerCount    = 0;
            project.RemainingProjectTime = project.TotalDeclaredTime;
            project.Status = true;

            _projectService.Add(project);


            foreach (var projectSection in projectCreationDto.ProjectSections)
            {
                var psection = new ProjectSection
                {
                    ProjectID          = project.ProjectID,
                    ProjectSectionName = projectSection.ProjectSectionName,
                    SectionProjectTime = projectSection.SectionProjectTime,
                };
                _projectSectionService.Add(psection);

                foreach (var projectSectionDepartment in projectSection.ProjectSectionDepartment)
                {
                    var psDepartment = new ProjectSectionDepartment
                    {
                        ProjectSectionID = psection.ProjectSectionID,
                        DepartmentTypeID = projectSectionDepartment.DepartmentTypeID,
                        Status           = true
                    };

                    _projectSectionDepartmentService.Add(psDepartment);
                }
            }

            return(new SuccessResult(Messages.ProjectAdded));
        }
        public async Task <IActionResult> Create([FromBody] ProjectCreationDto projectToCreate)
        {
            if (projectToCreate == null)
            {
                return(BadRequest());
            }

            if (await _unitOfWork.Projects.Exists(p => p.Name == projectToCreate.Name))
            {
                ModelState.AddModelError("Name", "The provided name already exists");
            }

            if (!Enum.IsDefined(typeof(ProjectType), Enum.ToObject(typeof(ProjectType), projectToCreate.Type)))
            {
                ModelState.AddModelError("Type", "The provided type is not a valid project type.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _unitOfWork.Users.SingleOrDefaultAsync(u => u.Id == projectToCreate.UserId);

            if (!_unitOfWork.Users.CanUserEdit(user))
            {
                return(Forbid());
            }

            var project = Mapper.Map <Project>(projectToCreate);

            project.SetCreatedDetails(user);
            foreach (var userId in projectToCreate.Users)
            {
                try
                {
                    var newUser = await _unitOfWork.Users.SingleOrDefaultAsync(u => u.Id == userId);

                    if (newUser != null)
                    {
                        project.AssignUser(newUser);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            project.Protocol = new Protocol
            {
                DateLastEdited = DateTime.UtcNow,
                LastEditedBy   = user
            };

            _unitOfWork.Projects.Add(project);

            if (await _unitOfWork.Complete() <= 0)
            {
                return(StatusCode(304));
            }

            var projectToReturn = Mapper.Map <ProjectReturnDto>(project);

            return(CreatedAtRoute("GetProjectById", new { id = project.Id }, projectToReturn));
        }