Ejemplo n.º 1
0
        public ProjectFrameworkM AddMany(Guid project_id, IList <Guid> framework_ids)
        {
            try
            {
                foreach (var framework_id in framework_ids)
                {
                    if (!_framework.Any(f => f.Id.Equals(framework_id)))
                    {
                        throw NotFound(framework_id, "framework id");
                    }
                }
                foreach (var framework_id in framework_ids)
                {
                    if (!_projectFramework.Any(pf => pf.ProjectId.Equals(project_id) && pf.FrameworkId.Equals(framework_id)))
                    {
                        _projectFramework.Add(new ProjectFramework
                        {
                            ProjectId   = project_id,
                            FrameworkId = framework_id
                        });
                    }
                }
                SaveChanges();

                return(GetDetail(project_id));
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add project frameworks!",
                                                                         e, DateTime.Now, "Server", "Service_ProjectFramework_AddMany");
            }
        }
Ejemplo n.º 2
0
        public IList <SprintM> GetAll(Guid project_id)
        {
            try
            {
                var result = _sprint.Where(s => s.ProjectId.Equals(project_id)).OrderByDescending(s => s.No)
                             .Select(s => new SprintM
                {
                    EndDate   = s.EndDate,
                    Id        = s.Id,
                    No        = s.No,
                    StartDate = s.StartDate,
                    Stage     = new Stage
                    {
                        StageCode = s.StageCode,
                        Name      = s.StageCode.ToString()
                    },
                    NextStage = new Stage
                    {
                        StageCode = StageUtils.GetNextStage(s.StageCode),
                        Name      = StageUtils.GetNextStage(s.StageCode).ToString()
                    }
                }).ToList();

                foreach (var sprint in result)
                {
                    sprint.IsRequireApproval = _approval.Any(a => a.SprintId.Equals(sprint.Id) && a.StageCode.Equals(sprint.NextStage.StageCode));
                }
                return(result);
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while get all sprint!",
                                                                         e, DateTime.Now, "Server", "Service_Sprint_GetAll");
            }
        }
Ejemplo n.º 3
0
 public void EnsureProjectMember(Guid user_id, Guid project_id)
 {
     try
     {
         if (!_permission.Any(p => p.UserId.Equals(user_id) && p.ProjectId.Equals(project_id)))
         {
             throw NotFound(project_id, "project id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure the project member!",
                                                                  e, DateTime.Now, "Server", "Service_Project_EnsureProjectMember");
     }
 }
Ejemplo n.º 4
0
        public LanguageM Add(LanguageCreateM model)
        {
            try
            {
                if (!("Server".Equals(model.Side) || "Client".Equals(model.Side) || "Database".Equals(model.Side)))
                {
                    throw BadRequest("Side value of framework must be 'Server', 'Client' or 'Database'!");
                }
                if (_language.Any(l => l.Name.Equals(model.Name) && l.Side.Equals(model.Side)))
                {
                    throw BadRequest("This language is existed!");
                }

                var language = _language.Add(new Language
                {
                    Name = model.Name,
                    Side = model.Side
                });
                SaveChanges();
                return(new LanguageM
                {
                    Id = language.Id,
                    Name = language.Name,
                    Side = language.Side
                });
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add a language!",
                                                                         e, DateTime.Now, "Server", "Service_Language_Add");
            }
        }
Ejemplo n.º 5
0
 public SprintTasksM GetAll(Guid sprint_id)
 {
     try
     {
         var result = _sprint.Where(s => s.Id.Equals(sprint_id))
                      .Select(s => new SprintTasksM
         {
             Id        = s.Id,
             EndDate   = s.EndDate,
             NextStage = new Models.Nococid.Stage
             {
                 Name      = StageUtils.GetNextStage(s.StageCode).ToString(),
                 StageCode = StageUtils.GetNextStage(s.StageCode)
             },
             Stage = new Models.Nococid.Stage
             {
                 StageCode = s.StageCode,
                 Name      = s.StageCode.ToString()
             },
             No        = s.No,
             StartDate = s.StartDate,
             Tasks     = s.StageCode.Equals(StageEnum.Planning) == true ? null : s.Tasks.Select(t => new TaskDM
             {
                 AssignedUser = new UserM
                 {
                     Id       = t.User.Id,
                     Username = t.User.Username
                 },
                 Id     = t.Id,
                 Detail = t.Detail,
                 Name   = t.Name,
                 Side   = t.Side,
                 Status = t.Status
             }).ToList()
         }).FirstOrDefault();
         if (result != null)
         {
             result.IsRequireApproval = _approval.Any(a => a.SprintId.Equals(result.Id) && a.StageCode.Equals(result.NextStage.StageCode));
         }
         return(result);
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while get all tasks!",
                                                                  e, DateTime.Now, "Server", "Service_Task_GetAll");
     }
 }
Ejemplo n.º 6
0
        public UserAuthorizationM Login(UserLoginM model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password))
                {
                    throw BadRequest("Username and Password must not empty!");
                }
                if (model.Username.Length < 3 || model.Password.Length < 3)
                {
                    throw BadRequest("Username and Password must have more than 3 characters!");
                }

                User user = _user.Where(u => u.Username.Equals(model.Username))
                            .Select(u => new User {
                    Id          = u.Id,
                    Username    = u.Username,
                    Password    = u.Password,
                    AdminUserId = u.AdminUserId
                }).FirstOrDefault();
                if (user == null)
                {
                    throw BadRequest("Username or password is incorrect!");
                }
                bool result = NococidAuthentication.VerifyHashedPassword(user.Username, user.Password, model.Password, out string rehashed_password);
                if (!result)
                {
                    throw BadRequest("Username or password is incorrect!");
                }

                if (rehashed_password != null)
                {
                    user.Password = rehashed_password;
                }
                SaveChanges();

                return(new UserAuthorizationM
                {
                    User = new UserM
                    {
                        Id = user.Id,
                        Username = user.Username
                    },
                    HasVscAccount = _account.Any(a => a.UserId.Equals(user.Id)),
                    AdminUser = user.AdminUserId == null ? null : _user.Where(u => u.Id.Equals(user.AdminUserId.Value)).Select(u => new UserM
                    {
                        Id = u.Id,
                        Username = u.Username
                    }).FirstOrDefault()
                });
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while log in!",
                                                                         e, DateTime.Now, "Server", "Service_User_Login");
            }
        }
Ejemplo n.º 7
0
        public IList <ProjectLastSprintM> GetAll(Guid user_id)
        {
            try
            {
                var result = _permission.Where(p => p.Project.Permissions.Any(p => p.UserId.Equals(user_id)) && p.RoleId.Equals(RoleID.Admin))
                             .Select(p => new ProjectLastSprintM
                {
                    CreatedDate = p.Project.CreatedDate,
                    EndDate     = p.Project.EndDate,
                    Id          = p.Project.Id,
                    Name        = p.Project.Name,
                    ProjectType = new ProjectTypeM
                    {
                        Id   = p.Project.ProjectType.Id,
                        Name = p.Project.ProjectType.Name
                    },
                    StartDate = p.Project.StartDate,
                    Owner     = new UserM
                    {
                        Id       = p.User.Id,
                        Username = p.User.Username
                    },
                    TotalSprint = p.Project.Sprints.Count(),
                    LastSprint  = p.Project.Sprints.OrderByDescending(s => s.No).Select(s => new SprintM
                    {
                        No        = s.No,
                        EndDate   = s.EndDate,
                        Id        = s.Id,
                        NextStage = new Models.Nococid.Stage
                        {
                            Name      = StageUtils.GetNextStage(s.StageCode).ToString(),
                            StageCode = StageUtils.GetNextStage(s.StageCode)
                        },
                        Stage = new Models.Nococid.Stage
                        {
                            StageCode = s.StageCode,
                            Name      = s.StageCode.ToString()
                        },
                        StartDate = s.StartDate
                    }).FirstOrDefault()
                }).ToList();

                foreach (var project in result)
                {
                    if (project.LastSprint != null)
                    {
                        project.LastSprint.IsRequireApproval = _approval.Any(a => a.SprintId.Equals(project.LastSprint.Id) && a.StageCode.Equals(project.LastSprint.NextStage.StageCode));
                    }
                }
                return(result);
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while get all project!",
                                                                         e, DateTime.Now, "Server", "Service_Project_GetAll");
            }
        }
Ejemplo n.º 8
0
 public bool HasHook(Guid repository_id)
 {
     try
     {
         return(_repository.Any(r => r.Id.Equals(repository_id) && r.HookId != null));
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure the repository's webhook is existed or not!",
                                                                  e, DateTime.Now, "Server", "Service_Repository_EnsureWebhookExisted");
     }
 }
Ejemplo n.º 9
0
 public bool HasCollab(Guid user_id, Guid repository_id)
 {
     try
     {
         return(_collaborator.Any(c => c.Account.UserId.Equals(user_id) && c.RepositoryId.Equals(repository_id)));
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while invite a member!",
                                                                  e, DateTime.Now, "Server", "Service_Collaborator_Add");
     }
 }
Ejemplo n.º 10
0
        public SprintTasksM AddMany(Guid project_id, Guid sprint_id, TaskSprintCreateM model)
        {
            try
            {
                foreach (var side in model.Sides)
                {
                    if (!("Server".Equals(side.Side) || "Client".Equals(side.Side) || "Database".Equals(side.Side)))
                    {
                        throw BadRequest("Side value must be 'Server', 'Client' or 'Database'!");
                    }
                    if (side.Tasks.Count == 0)
                    {
                        throw BadRequest("No task is assigned!");
                    }
                    foreach (var task in side.Tasks)
                    {
                        if (!_permission.Any(p => p.ProjectId.Equals(project_id) && p.UserId.Equals(task.AssignedUserId)))
                        {
                            throw NotFound(task.AssignedUserId, "assigned user id");
                        }
                    }
                }

                foreach (var side in model.Sides)
                {
                    foreach (var task in side.Tasks)
                    {
                        _task.Add(new Data.Models.Task
                        {
                            Detail   = task.Detail,
                            IsDelete = false,
                            Name     = task.Name,
                            SprintId = sprint_id,
                            Status   = "Incomplete",
                            UserId   = task.AssignedUserId,
                            Side     = side.Side
                        });
                    }
                }
                SaveChanges();

                return(GetAll(sprint_id));
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add tasks!",
                                                                         e, DateTime.Now, "Server", "Service_Task_AddMany");
            }
        }
Ejemplo n.º 11
0
 public void EnsureExisted(Guid user_id, Guid commit_id)
 {
     try
     {
         if (!_commit.Any(c => c.Account.UserId.Equals(user_id) && c.Id.Equals(commit_id)))
         {
             throw NotFound(commit_id, "commit id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure a commit existed!",
                                                                  e, DateTime.Now, "Server", "Service_Commit_EnsureExisted");
     }
 }
Ejemplo n.º 12
0
 public void EnsureOwner(Guid user_id, Guid account_id)
 {
     try
     {
         if (!_account.Any(a => a.Id.Equals(account_id) && a.UserId.Equals(user_id)))
         {
             throw NotFound(account_id, "account id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure the third party owner!",
                                                                  e, DateTime.Now, "Server", "Service_Account_EnsureOwner");
     }
 }
Ejemplo n.º 13
0
 public void EnsureExisted(Guid project_id, Guid sprint_id)
 {
     try
     {
         if (!_sprint.Any(s => s.Id.Equals(sprint_id) && s.ProjectId.Equals(project_id)))
         {
             throw NotFound(sprint_id, "sprint id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure the sprint of project!",
                                                                  e, DateTime.Now, "Server", "Service_Sprint_EnsureExisted");
     }
 }
 public void EnsureExist(Guid project_id, Guid repository_id)
 {
     try
     {
         if (!_projectRepository.Any(pr => pr.ProjectId.Equals(project_id) && pr.RepositoryId.Equals(repository_id)))
         {
             throw NotFound();
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure project repository existed!",
                                                                  e, DateTime.Now, "Server", "Service_ProjectRepository_EnsureExist");
     }
 }
 public void EnsureToolExisted(Guid tool_id)
 {
     try
     {
         if (!_tool.Any(t => t.Id.Equals(tool_id)))
         {
             throw NotFound();
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred!",
                                                                  e, DateTime.Now, "Server", "Service-Configuration-EnsureToolExisted");
     }
 }
Ejemplo n.º 16
0
 public void EnsureExisted(Guid sprint_id, Guid workflow_id)
 {
     try
     {
         if (!_workflow.Any(w => w.SprintId.Equals(sprint_id) && w.Id.Equals(workflow_id)))
         {
             throw NotFound(workflow_id, "workflow id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure the workflow!",
                                                                  e, DateTime.Now, "Server", "Service_WorkFlow_EnsureExisted");
     }
 }
Ejemplo n.º 17
0
 public void EnsureExisted(Guid sprint_id, Guid task_id)
 {
     try
     {
         if (!_task.Any(t => t.SprintId.Equals(sprint_id) && t.Id.Equals(task_id)))
         {
             throw NotFound(task_id, "task id");
         }
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while ensure a tas kis existed!",
                                                                  e, DateTime.Now, "Server", "Service_Task_EnsureExisted");
     }
 }
Ejemplo n.º 18
0
        public UserAuthorizationM Register(UserCreateM model, Guid?admin_user_id)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Username) | string.IsNullOrEmpty(model.Password))
                {
                    throw BadRequest("The username or password not must emplty!");
                }
                if (_user.Any(u => u.Username.Equals(model.Username)))
                {
                    throw BadRequest("The username has been used!");
                }

                User user = _user.Add(new User
                {
                    Username    = model.Username,
                    Password    = NococidAuthentication.GetHashedPassword(model.Username, model.Password),
                    AdminUserId = admin_user_id
                });
                SaveChanges();
                UserAuthorizationM result = new UserAuthorizationM
                {
                    HasVscAccount = false,
                    User          = new UserM
                    {
                        Id       = user.Id,
                        Username = user.Username
                    }
                };
                if (admin_user_id != null)
                {
                    result.AdminUser = _user.Where(u => u.Id.Equals(admin_user_id))
                                       .Select(u => new UserM
                    {
                        Id       = u.Id,
                        Username = u.Username
                    }).FirstOrDefault();
                }

                return(result);
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while register!",
                                                                         e, DateTime.Now, "Server", "Service_User_Register");
            }
        }
Ejemplo n.º 19
0
        public object GetForCommit(Guid user_id, Guid commit_id)
        {
            try
            {
                if (!_commit.Any(c => c.Account.UserId.Equals(user_id) && c.Id.Equals(commit_id)))
                {
                    throw NotFound(commit_id, "commit id");
                }

                return(_commit.Where(c => c.Id.Equals(commit_id))
                       .Select(c => c.Branch.Repository.ProjectRepositories
                               .Select(pr => new
                {
                    c.Id,
                    c.Message,
                    c.MessageBody,
                    c.CommitTime,
                    c.IsSubmit,
                    Project = new
                    {
                        pr.Project.Id,
                        pr.Project.Name,
                        ProjectType = pr.Project.ProjectType.Name,
                        Sprint = pr.Project.Sprints.Where(s => s.StageCode.Equals(StageEnum.Coding)).Select(s => new
                        {
                            s.Id,
                            s.No,
                            Tasks = s.Tasks.Where(t => t.SprintId.Equals(s.Id) && t.UserId.Equals(user_id) && (t.Status.Equals("Incomplete") || t.Status.Equals("Error")))
                                    .Select(t => new
                            {
                                t.Id,
                                t.Name,
                                t.Detail,
                                t.Status,
                                t.Side
                            }).ToList()
                        }).FirstOrDefault()
                    }
                }).FirstOrDefault())
                       .FirstOrDefault());
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred!",
                                                                         e, DateTime.Now, "Server", "Service_Task_GetForSubmit");
            }
        }
Ejemplo n.º 20
0
 public void Delete(Guid language_id)
 {
     try
     {
         if (_projectFramework.Any(pf => pf.Framework.LanguageId.Equals(language_id)))
         {
             throw BadRequest("A project is refering to a framework of this language!");
         }
         Language language = _language.Where(l => l.Id.Equals(language_id)).Include(l => l.Frameworks).FirstOrDefault();
         _framework.DeleteAll(language.Frameworks);
         _language.Remove(language);
         SaveChanges();
     }
     catch (Exception e)
     {
         throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while delete a language!",
                                                                  e, DateTime.Now, "Server", "Service_Language_Delete");
     }
 }
Ejemplo n.º 21
0
        public FrameworkDM Add(Guid language_id, FrameworkCreateM model)
        {
            try
            {
                Language language = _language.GetOne(l => l.Id.Equals(language_id));
                if (language == null)
                {
                    throw NotFound(language_id, "language id");
                }
                if (_framework.Any(f => f.Name.Equals(model.Name) && f.LanguageId.Equals(language_id)))
                {
                    throw BadRequest("This language is already existed!");
                }

                var framework = _framework.Add(new Framework
                {
                    LanguageId = language_id,
                    Name       = model.Name
                });
                SaveChanges();

                return(new FrameworkDM
                {
                    Language = new LanguageDM
                    {
                        Id = language.Id,
                        Name = language.Name,
                        Side = language.Side
                    },
                    Name = framework.Name,
                    Id = framework.Id
                });
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add a framework!",
                                                                         e, DateTime.Now, "Server", "Service_Framework_Add");
            }
        }
Ejemplo n.º 22
0
        public IList <RepositoryM> AddManyGH(Guid account_id, IList <GHRepository> gh_repositories)
        {
            try
            {
                IList <Account>      new_accounts = new List <Account>();
                Account              owner;
                Repository           repository;
                IList <Collaborator> new_collaborator = new List <Collaborator>();
                foreach (var gh_repository in gh_repositories)
                {
                    repository = _repository.Where(r => r.ThirdPartyRepositoryId.Equals(gh_repository.Id.ToString())).FirstOrDefault();
                    if (repository == null)
                    {
                        repository = _repository.Add(new Repository
                        {
                            Languages = gh_repository.Languages,
                            Name      = gh_repository.Name,
                            ThirdPartyRepositoryId = gh_repository.Id.ToString()
                        });
                    }

                    owner = _account.Where(a => a.ThirdPartyAccountId.Equals(gh_repository.Owner.Id.ToString())).FirstOrDefault();
                    if (owner == null)
                    {
                        owner = _account.Add(new Account
                        {
                            AvatarUrl           = gh_repository.Owner.Avatar_url,
                            IsMain              = false,
                            ThirdPartyAccountId = gh_repository.Owner.Id.ToString(),
                            Name   = gh_repository.Owner.Login,
                            ToolId = ToolID.Github
                        });
                    }

                    if (!_collaborator.Any(c => c.AccountId.Equals(account_id) && c.RepositoryId.Equals(repository.Id)))
                    {
                        _collaborator.Add(new Collaborator
                        {
                            AccountId    = account_id,
                            OwnerId      = owner.Id,
                            RepositoryId = repository.Id
                        });
                    }

                    if (!owner.Id.Equals(account_id))
                    {
                        if (!_collaborator.Any(c => c.AccountId.Equals(owner.Id) && c.RepositoryId.Equals(repository.Id)))
                        {
                            _collaborator.Add(new Collaborator
                            {
                                OwnerId      = owner.Id,
                                AccountId    = owner.Id,
                                RepositoryId = repository.Id
                            });
                        }
                    }

                    SaveChanges();
                }
                return(GetAll(account_id));
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add repositories!",
                                                                         e, DateTime.Now, "Server", "Service_Repository_AddMany");
            }
        }
        public IList <ProjectRepositoryM> SetProjectRepository(Guid user_id, Guid project_id, ProjectRepositorySetupM model)
        {
            try
            {
                if (!("Server".Equals(model.Side) || "Client".Equals(model.Side) || "Database".Equals(model.Side)))
                {
                    throw BadRequest("Side value must be 'Server' or 'Client'!");
                }

                if (!_collaborator.Any(c => c.Account.UserId.Equals(user_id) && c.RepositoryId.Equals(model.RepositoryId) && c.AccountId.Equals(c.OwnerId)))
                {
                    throw NotFound(model.RepositoryId, "repository id");
                }

                ProjectRepository project_repository = _projectRepository.GetOne(pr => pr.ProjectId.Equals(project_id) && pr.Side.Equals(model.Side));
                var project = _projectRepository.Where(pr => pr.RepositoryId.Equals(model.RepositoryId) && pr.Side.Equals(model.Side))
                              .Select(pr => new
                {
                    pr.Project.Id,
                    pr.Project.Name
                }).FirstOrDefault();
                if (project_repository == null)
                {
                    if (project != null)
                    {
                        throw BadRequest("This repository has been set to project " + project.Name + "!");
                    }
                    else
                    {
                        _projectRepository.Add(new ProjectRepository
                        {
                            ProjectId    = project_id,
                            RepositoryId = model.RepositoryId,
                            Side         = model.Side
                        });
                        SaveChanges();
                    }
                }
                else
                {
                    if (project != null)
                    {
                        if (!project.Id.Equals(project_id))
                        {
                            throw BadRequest("This repository has been set to project " + project.Name + "!");
                        }
                    }
                    if (!project_repository.RepositoryId.Equals(model.RepositoryId))
                    {
                        _projectRepository.Remove(project_repository);
                        _projectRepository.Add(new ProjectRepository
                        {
                            ProjectId    = project_id,
                            RepositoryId = model.RepositoryId,
                            Side         = model.Side
                        });
                        SaveChanges();
                    }
                }

                return(GetProjectRepositories(project_id));
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while set project repository!",
                                                                         e, DateTime.Now, "Server", "Service_ProjectRepository_SetProjectRepository");
            }
        }
Ejemplo n.º 24
0
        public ProjectM Add(Guid admin_user_id, Guid user_id, ProjectCreateM model)
        {
            try
            {
                if (!admin_user_id.Equals(Guid.Empty))
                {
                    throw Forbidden();
                }
                if (model.Name.Contains("/"))
                {
                    throw BadRequest("Project name can not contain slash(/)!");
                }
                ProjectType project_type = _projectType.GetOne(p => p.Id.Equals(model.ProjectTypeId));
                if (project_type == null)
                {
                    throw NotFound(model.ProjectTypeId, "project type id");
                }
                if (_project.Any(p => p.Name.Equals(model.Name) && p.Permissions.Any(p => p.UserId.Equals(user_id) && p.RoleId.Equals(RoleID.Admin))))
                {
                    throw BadRequest("The project name is already existed!");
                }

                Project project = _project.Add(new Project
                {
                    ProjectTypeId = model.ProjectTypeId,
                    IsDelete      = false,
                    Name          = model.Name,
                    StartDate     = model.StartDate,
                    CreatedDate   = DateTime.Now,
                    EndDate       = model.EndDate
                });
                _permission.Add(new Permission
                {
                    UserId    = user_id,
                    ProjectId = project.Id,
                    RoleId    = RoleID.Admin
                });
                SaveChanges();

                return(new ProjectM
                {
                    Id = project.Id,
                    CreatedDate = project.CreatedDate,
                    EndDate = project.EndDate,
                    Name = project.Name,
                    StartDate = project.StartDate,
                    ProjectType = new ProjectTypeM
                    {
                        Id = project_type.Id,
                        Name = project_type.Name
                    },
                    Owner = _user.Where(u => u.Id.Equals(user_id)).Select(u => new UserM
                    {
                        Id = u.Id,
                        Username = u.Username
                    }).FirstOrDefault()
                });
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add project!",
                                                                         e, DateTime.Now, "Server", "Service_Project_Add");
            }
        }