public UpdateResponse <ProjectDTO> Update(string userId, Guid id, UpdateProjectRequest req)
        {
            var res = new UpdateResponse <ProjectDTO>();

            var project = _projectRepository.FindById(id);

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

            var owners = project.ProjectUsersReq.Where(pur => pur.Role.RoleName == "PROJECT_MANAGER").ToList();


            if (owners.Find(pur => pur.Sender.Id.Equals(Guid.Parse(userId)) ||
                            pur.UserAssigned.Id.Equals(Guid.Parse(userId))) == null)
            {
                return((UpdateResponse <ProjectDTO>)res.ReturnErrorResponseWith("Only project managers can update projects!"));
            }

            project.Name        = req.Name;
            project.Description = req.Description;
            project.Closed      = req.Closed;
            project.Deadline    = req.Deadline;

            project.Validate();

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

            _projectRepository.Update(project);

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

            res.Success   = true;
            res.EntityDTO = _mapper.Map <Project, ProjectDTO>(project);
            return(res);
        }
        public UpdateResponse <UserDTO> Update(UpdateUserRequest req)
        {
            var res  = new UpdateResponse <UserDTO>();
            var user = _userRepository.FindById(req.Id);

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

            if (req.FirstName == null || req.LastName == null)
            {
                return((UpdateResponse <UserDTO>)res.ReturnErrorResponseWith("Bad request"));
            }

            user.FirstName = req.FirstName;
            user.LastName  = req.LastName;

            user.Validate();

            if (user.GetBrokenRules().Count > 0)
            {
                return((UpdateResponse <UserDTO>)res.ReturnErrorResponseWithMultiple(user.GetBrokenRules()));
            }

            try
            {
                _userRepository.Update(user);
                _uow.Commit();
            }
            catch (Exception ex)
            {
                return((UpdateResponse <UserDTO>)res.ReturnErrorResponseWith(ex.Message));
            }

            res.EntityDTO = _mapper.Map <User, UserDTO>(user);
            res.Success   = true;
            return(res);
        }
Exemple #3
0
        public UpdateResponse <TicketDTO> Update(Guid id, Guid userId, UpdateTicketRequest req)
        {
            var res = new UpdateResponse <TicketDTO>();

            var type = req.Type.ConvertToTicketType();

            var ticket = _ticketRepository.FindById(id);

            if (ticket == null)
            {
                return((UpdateResponse <TicketDTO>)res.ReturnErrorResponseWith("Ticket not found"));
            }

            if (ticket.Project.ProjectUsersReq.Where(
                    pur => pur.UserAssigned.Id.Equals(userId) ||
                    pur.Sender.Id.Equals(userId)).ToList().Count <= 0)
            {
                return((UpdateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("You can cnahge only tickets on projects that you are working on!"));
            }

            var user = _userRepository.FindById(userId);

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

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

            if (status == null)
            {
                return((UpdateResponse <TicketDTO>)
                       res.ReturnErrorResponseWith("Status not found"));
            }

            var histories = _ticketDomainService
                            .GetHistoriesFor(ticket,
                                             user,
                                             req.Title,
                                             req.Description,
                                             req.Deadline,
                                             type,
                                             status);


            if (type != TicketType.UNDEFINED)
            {
                ticket.Type = type;
            }


            ticket.Title       = req.Title;
            ticket.Description = req.Description;
            ticket.Deadline    = req.Deadline;
            ticket.Status      = status;

            ticket.Validate();

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

            ticket.Updated = DateTime.Now;

            foreach (var history in histories)
            {
                _ticketHistoryRepository.Save(history);
            }

            _ticketRepository.Update(ticket);

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

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

            return(res);
        }