Exemplo n.º 1
0
        public CreateResponse <ProjectDTO> Create(string id, CreateProjectRequest req)
        {
            var res = new CreateResponse <ProjectDTO>();

            var user = _userRepository.FindById(Guid.Parse(id));

            Console.WriteLine(user);

            if (user == null)
            {
                return((CreateResponse <ProjectDTO>)res.ReturnErrorResponseWith("User not found."));
            }

            var role = _roleRepository.FindRoleByName("PROJECT_MANAGER");

            if (role == null)
            {
                return((CreateResponse <ProjectDTO>)res.ReturnErrorResponseWith("Error ocurred during asigning roles"));
            }

            var project = _projectDomainService.CreateProject(user,
                                                              req.Name,
                                                              req.Description,
                                                              req.Deadline,
                                                              role);



            project.Validate();

            if (project.GetBrokenRules().Count > 0)
            {
                return((CreateResponse <ProjectDTO>)res.ReturnErrorResponseWithMultiple(project.GetBrokenRules()));
            }

            _projectRepository.Save(project);

            try
            {
                _uow.Commit();
            }
            catch (Exception ex)
            {
                return((CreateResponse <ProjectDTO>)res.ReturnErrorResponseWith(ex.Message));
            }

            res.Success   = true;
            res.EntityDTO = _mapper.Map <Project, ProjectDTO>(project);
            return(res);
        }
Exemplo n.º 2
0
        public CreateResponse <TicketDTO> Create(Guid id, CreateTicketRequest req)
        {
            var res = new CreateResponse <TicketDTO>();

            var project = _projectRepository.FindById(req.ProjectId);

            if (project == null)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("Specified project doesn't exist"));
            }

            var user = _userRepository.FindById(id);

            if (user == null)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("Specified user doesn't exist"));
            }

            var projectUser = _projectUserReqRepository
                              .FindProjectUserFor(user.Id, project.Id);

            if (projectUser == null)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("Only users on projects can make new tickets"));
            }

            var ticketStatus = _ticketStatusRepository.FindById(req.StatusId);

            if (ticketStatus == null)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("Specified ticket status doesn't exist"));
            }

            var ticketType = req.TicketType == null ?
                             TicketType.UNDEFINED :
                             req.TicketType.ConvertToTicketType();

            var ticket = _mapper.Map <CreateTicketRequest, Ticket>(req);

            ticket.Reporter = projectUser;
            ticket.Project  = project;
            ticket.Status   = ticketStatus;
            ticket.Created  = DateTime.Now;
            ticket.Type     = ticketType;

            Console.WriteLine(ticket);

            ticket.Validate();

            if (ticket.GetBrokenRules().Count > 0)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWithMultiple(ticket.GetBrokenRules()));
            }

            _ticketRepository.Save(ticket);

            try
            {
                _uow.Commit();
            }
            catch (Exception ex)
            {
                return((CreateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith(ex.Message));
            }


            res.Success   = true;
            res.EntityDTO = _mapper.Map <Ticket, TicketDTO>(ticket);

            return(res);
        }
        public CreateResponse <ProjectUserRequestDTO> Create(Guid id, CreateProjectUserReqRequest req)
        {
            var res = new CreateResponse <ProjectUserRequestDTO>();

            var role = _roleRepository.FindById(req.RoleId);

            if (role == null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("Role not found."));
            }

            var project = _projectRepository.FindById(req.ProjectId);

            if (project == null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("Project not found."));
            }

            var sender = _userRepository.FindById(id);

            if (sender == null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("Invalid sender."));
            }

            var userAssigned = _userRepository.FindById(req.UserAssignedId);

            if (userAssigned == null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("Assigned user not found."));
            }

            var projectManagers = project.ProjectUsersReq
                                  .Where(pur => pur.Accepted == true && pur.Role.RoleName == "PROJECT_MANAGER").ToList();

            if (projectManagers.Find(pur => pur.UserAssigned.Id == id) == null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("User must be project manager to send requests."));
            }

            if (project.ProjectUsersReq.ToList().Find(pur => pur.UserAssigned.Id == req.UserAssignedId) != null)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith("User has alredy received an invitation."));
            }

            var projectUserReq = new ProjectUserReq()
            {
                Id           = Guid.NewGuid(),
                Message      = req.Message,
                UserAssigned = userAssigned,
                Sender       = sender,
                Role         = role,
                Project      = project,
                Accepted     = false,
                RequestSent  = DateTime.Now
            };

            projectUserReq.Validate();

            if (projectUserReq.GetBrokenRules().Count > 0)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWithMultiple(projectUserReq.GetBrokenRules()));
            }

            _projectUserReqRepository.Save(projectUserReq);

            try
            {
                _uow.Commit();
            }
            catch (Exception ex)
            {
                return((CreateResponse <ProjectUserRequestDTO>)
                       res.ReturnErrorResponseWith(ex.Message));
            }

            res.Success = true;
            return(res);
        }