public JsonResult QueryTotalUser(int timeInterval, int itemCount)
        {
            if (timeInterval <= 0)
            {
                return(JsonReturnHelper.ErrorReturn(457, "Invalid timeInterval."));
            }

            var currentTotalUser = _meshContext.Users.Count();

            var historyTotalUser = new List <UserCountInfo>();

            for (var i = 0; i < itemCount; ++i)
            {
                var totalUser = _meshContext.Users
                                .Count(u => u.CreatedTime.Date < DateTime.Now.Date.AddDays(-i * timeInterval) &&
                                       u.CreatedTime.Date >= DateTime.Now.Date.AddDays(-(i + 1) * timeInterval));
                historyTotalUser.Add(new UserCountInfo()
                {
                    TotalUser = totalUser
                });
            }

            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    isSuccess = true,
                    msg = "",
                    currentTotalUser = currentTotalUser,
                    historyTotalUser = historyTotalUser
                }
            }));
        }
Ejemplo n.º 2
0
        public JsonResult QueryProject(string username, int projectId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            //Check if project exists
            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Invalid projectId."));
            }

            //Check if user is in the project
            var projectCheckResult = _permissionCheck.CheckProjectPermission(username, project);

            if (projectCheckResult == PermissionCheckHelper.ProjectOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var user = _meshContext.Users.First(u => u.Id == project.AdminId);

            return(ProjectResult(project, user.Nickname));
        }
Ejemplo n.º 3
0
        public JsonResult Login(UserRequest request)
        {
            if (!CornerCaseCheckHelper.Check(request.username, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(104, "Invalid username"));
            }

            if (!CornerCaseCheckHelper.Check(request.password, 0, CornerCaseCheckHelper.PassWord))
            {
                return(JsonReturnHelper.ErrorReturn(111, "Invalid password."));
            }

            var user = _meshContext.Users.FirstOrDefault(u => u.Email == request.username);

            //Check Password
            if (user == null || !CheckHashPassword(request.password, user.PasswordSalt, user.PasswordDigest))
            {
                return(JsonReturnHelper.ErrorReturn(201, "Incorrect username or password."));
            }

            if (HttpContext.Session.IsAvailable && HttpContext.Session.GetString(request.username) == null)
            {
                HttpContext.Session.SetString(request.username, Guid.NewGuid().ToString());
            }

            return(UserReturnValue(user));
        }
        public JsonResult PreferenceLayout(PreferenceRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.preferenceLayout, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(111, "Invalid preferenceLayout"));
            }

            var user = _meshContext.Users.First(u => u.Email == request.username);

            try
            {
                user.LayoutPreference = request.preferenceLayout;
                _meshContext.Users.Update(user);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 5
0
 public JsonResult CheckUsername(string username)
 {
     if (!CornerCaseCheckHelper.Check(username, 50, CornerCaseCheckHelper.Username))
     {
         return(JsonReturnHelper.ErrorReturn(104, "Invalid username."));
     }
     return(HttpContext.Session.GetString(username) == null?JsonReturnHelper.ErrorReturn(2, "User status error.") : null);
 }
Ejemplo n.º 6
0
        public JsonResult DeleteTeamKB(string username, int teamId, int knowledgeId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId."));
            }

            if (!CornerCaseCheckHelper.Check(knowledgeId, 50, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(807, "Invalid knowledgeId."));
            }

            var team = _meshContext.Teams.FirstOrDefault(p => p.Id == teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Team does not exist."));
            }

            var memoCollection = _meshContext.TeamMemoCollections.First(p => p.TeamId == teamId);
            var knowledge      =
                _meshContext.TeamMemos.FirstOrDefault(
                    m => m.Id == knowledgeId && m.CollectionId == memoCollection.Id);

            if (knowledge == null)
            {
                return(JsonReturnHelper.ErrorReturn(805, "Invalid knowledgeId."));
            }

            var user = _meshContext.Users.First(u => u.Email == username);

            if (_permissionCheck.CheckTeamPermission(username, team) != PermissionCheckHelper.TeamAdmin ||
                knowledge.UserId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(801, "Permission denied."));
            }

            try
            {
                _meshContext.TeamMemos.Remove(knowledge);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 7
0
        public JsonResult DeleteTask(string username, int projectId, int taskId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            if (!CornerCaseCheckHelper.Check(taskId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(501, "Invalid taskId."));
            }


            var user = _meshContext.Users.First(u => u.Email == username);

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Project does not exist."));
            }

            var task = _meshContext.Tasks.FirstOrDefault(t => t.Id == taskId);

            if (task == null)
            {
                return(JsonReturnHelper.ErrorReturn(604, "Task does not exist."));
            }

            if (_permissionCheck.CheckProjectPermission(username, project) != PermissionCheckHelper.ProjectAdmin &&
                task.LeaderId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            try
            {
                _meshContext.Tasks.Remove(task);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 8
0
        public JsonResult DeleteBulletin(string username, int projectId, int bulletinId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(bulletinId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid bulletinId."));
            }


            if (!CornerCaseCheckHelper.Check(projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            //Check if target bulletin exists
            var user    = _meshContext.Users.First(u => u.Email == username);
            var project = _meshContext.Projects.Find(projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(420, "Project does not exist."));
            }
            var bulletin = _meshContext.Bulletins.Find(bulletinId);

            if (bulletin == null)
            {
                return(JsonReturnHelper.ErrorReturn(401, "Bulletin does not exist."));
            }

            if (project.AdminId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(422, "Permission denied."));
            }

            try
            {
                _meshContext.Bulletins.Remove(bulletin);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 9
0
        public JsonResult QueryUser(string username, string keyword)
        {
            if (!CornerCaseCheckHelper.Check(username, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(104, "Invalid username."));
            }

            if (!CornerCaseCheckHelper.Check(keyword, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(457, "Invalid keyword."));
            }

            if (HttpContext.Session.IsAvailable && HttpContext.Session.GetString(username) == null)
            {
                return(JsonReturnHelper.ErrorReturn(2, "User status error."));
            }

            var userAccordingToUsername = _meshContext.Users
                                          .Where(u => u.Email.Contains(keyword));
            var userAccordingToNickname = _meshContext.Users
                                          .Where(u => u.Nickname.Contains(keyword));
            var userAccordingToDescription = _meshContext.Users
                                             .Where(u => u.Description.Contains(keyword));

            var users = userAccordingToUsername
                        .Union(userAccordingToNickname)
                        .Union(userAccordingToDescription)
                        .Select(u => new
            {
                username    = u.Email,
                avatar      = AvatarSaveHelper.GetObject(u.Avatar),
                nickname    = u.Nickname,
                gender      = u.Gender,
                status      = u.Status,
                address     = u.Address,
                description = u.Description,
                birthday    = u.Birthday
            })
                        .ToList();


            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    users = users
                }
            }));
        }
Ejemplo n.º 10
0
        public JsonResult CheckBulleti(string title, string description)
        {
            if (title.IsNullOrEmpty() || title.Length > 50)
            {
                return(JsonReturnHelper.ErrorReturn(402, "Invalid bulletinName."));
            }

            if (description.IsNullOrEmpty() || description.Length > 100)
            {
                return(JsonReturnHelper.ErrorReturn(403, "Invalid Description."));
            }

            return(null);
        }
Ejemplo n.º 11
0
        public JsonResult UpdateUserInformation(UserInfo request)
        {
            if (!CornerCaseCheckHelper.Check(request.username, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(104, "Invalid username"));
            }

            if (HttpContext.Session.GetString(request.username) == null)
            {
                return(JsonReturnHelper.ErrorReturn(2, "User status error."));
            }

            if (!CornerCaseCheckHelper.Check(request.nickname, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(120, "Invalid nickname."));
            }

            if (!CornerCaseCheckHelper.Check(request.birthday, 0, CornerCaseCheckHelper.Time))
            {
                return(JsonReturnHelper.ErrorReturn(121, "Invalid birthday."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(122, "Invalid description."));
            }

            var user = _meshContext.Users.First(u => u.Email == request.username);

            try
            {
                user.Nickname    = request.nickname;
                user.Gender      = request.gender;
                user.Status      = request.status;
                user.Address     = request.address;
                user.Description = request.description;
                user.Birthday    = Convert.ToDateTime(request.birthday);
                user.Avatar      = AvatarSaveHelper.PutObject(request.avatar, user.Avatar);
                _meshContext.Users.Update(user);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(UserReturnValue(user));
        }
Ejemplo n.º 12
0
        public JsonResult UpdateUserPassword(UserRequest request)
        {
            if (!CornerCaseCheckHelper.Check(request.username, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(104, "Invalid username"));
            }

            if (!CornerCaseCheckHelper.Check(request.oldPassword, 0, CornerCaseCheckHelper.PassWord))
            {
                return(JsonReturnHelper.ErrorReturn(112, "Invalid oldPassword."));
            }

            if (!CornerCaseCheckHelper.Check(request.password, 0, CornerCaseCheckHelper.PassWord))
            {
                return(JsonReturnHelper.ErrorReturn(111, "Invalid password."));
            }

            if (request.password == request.oldPassword)
            {
                return(JsonReturnHelper.ErrorReturn(113, "The old and new passwords are the same."));
            }

            var user = _meshContext.Users.FirstOrDefault(u => u.Email == request.username);

            //Check Password
            if (user == null || !CheckHashPassword(request.oldPassword, user.PasswordSalt, user.PasswordDigest))
            {
                return(JsonReturnHelper.ErrorReturn(201, "Incorrect username or password."));
            }
            HashPassword hashPassword = GetHashPassword(request.password);

            try
            {
                user.PasswordDigest = hashPassword.PasswordDigest;
                user.PasswordSalt   = hashPassword.PasswordSalt;
                _meshContext.Users.Update(user);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 13
0
        public JsonResult Register(UserRequest request)
        {
            if (!CornerCaseCheckHelper.Check(request.username, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(104, "Invalid username"));
            }

            if (!CornerCaseCheckHelper.Check(request.password, 0, CornerCaseCheckHelper.PassWord))
            {
                return(JsonReturnHelper.ErrorReturn(111, "Invalid password."));
            }

            var user = _meshContext.Users.FirstOrDefault(u => u.Email == request.username);

            if (user != null)
            {
                return(JsonReturnHelper.ErrorReturn(101, "User already exists."));
            }
            HashPassword hashPassword = GetHashPassword(request.password);
            //Create new user
            var newUser = new User()
            {
                Email              = request.username,
                Nickname           = request.username,
                PasswordDigest     = hashPassword.PasswordDigest,
                PasswordSalt       = hashPassword.PasswordSalt,
                Avatar             = AvatarSaveHelper.PutObject(""),
                ColorPreference    = "blue",
                LayoutPreference   = "default",
                RevealedPreference = "card"
            };

            //try to save the user
            try
            {
                _meshContext.Users.Add(newUser);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(UserReturnValue(newUser));
        }
Ejemplo n.º 14
0
        public JsonResult QueryTeamKB(string username, int teamId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId."));
            }


            var team = _meshContext.Teams.FirstOrDefault(p => p.Id == teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Team does not exist."));
            }

            if (_permissionCheck.CheckTeamPermission(username, team) == PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(801, "Permission denied."));
            }

            var collectionId = _meshContext.TeamMemoCollections.First(c => c.TeamId == team.Id).Id;
            var memoLists    = _meshContext.TeamMemos
                               .Where(m => m.CollectionId == collectionId)
                               .Join(_meshContext.Users, m => m.UserId, u => u.Id, (m, u) => new MemoInfo()
            {
                KnowledgeId   = m.Id,
                KnowledgeName = m.Title,
                HyperLink     = m.Text,
                CreateTime    = m.CreatedTime,
                UploaderName  = u.Nickname
            }).ToList();

            return(MemoListResult(memoLists));
        }
Ejemplo n.º 15
0
        public JsonResult QueryBulletin(string username, int projectId)
        {
            var checkUsername = CheckUsername(username);

            if (checkUsername != null)
            {
                return(checkUsername);
            }

            //Find the target project
            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            var bulletins = _meshContext.BulletinBoards
                            .Where(b => b.ProjectId == projectId)
                            .Join(_meshContext.Bulletins, bb => bb.Id, b => b.BoardId, (bb, b) => new
            {
                Id          = b.Id,
                Name        = b.Title,
                Content     = b.Content,
                BoardId     = b.BoardId,
                CreatedTime = b.CreatedTime
            }).ToList();

            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    isSuccess = true,
                    msg = "",
                    bulletins = bulletins
                }
            }));
        }
Ejemplo n.º 16
0
        public JsonResult CreateBulletin(BulletinRequest request)
        {
            var checkUsername = CheckUsername(request.username);

            if (checkUsername != null)
            {
                return(checkUsername);
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(402, "Invalid bulletinName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(403, "Invalid Description."));
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            var project = _meshContext.Projects.FirstOrDefault(u => u.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(411, "Project does not exist."));
            }

            //Find bulletinBoard of this project
            var bulletinBoard = _meshContext.BulletinBoards.First(b => b.ProjectId == request.projectId);
            var user          = _meshContext.Users.First(u => u.Email == request.username);

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(421, "Permission denied."));
            }

            //Check if the bulletin already exists.
            var bulletin =
                _meshContext.Bulletins.FirstOrDefault(b => b.Title == request.bulletinName && b.BoardId == bulletinBoard.Id);

            if (bulletin != null)
            {
                return(JsonReturnHelper.ErrorReturn(411, "Bulletin already exists."));
            }

            //Create the bulletin
            var newBulletin = new Bulletin()
            {
                Title   = request.bulletinName,
                Content = request.description,
                BoardId = bulletinBoard.Id
            };

            //Update feed
            var feedUsers = _meshContext.Develops
                            .Where(d => d.ProjectId == request.projectId)
                            .ToList();

            //Start Transaction to save the bulletin
            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Bulletins.Add(newBulletin);
                    _meshContext.SaveChanges();

                    var bulletinId = newBulletin.Id;
                    var feedList   = new List <BulletinFeed>();
                    foreach (var feedUser in feedUsers)
                    {
                        feedList.Add(new BulletinFeed()
                        {
                            UserId     = feedUser.UserId,
                            BulletinId = bulletinId
                        });
                    }

                    _meshContext.AddRange(feedList);
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }

            return(Json(new
            {
                err_code = 0,
                isSuccess = true,
                msg = "",
                bulletin = new
                {
                    bulletinId = newBulletin.Id,
                    bullentinName = newBulletin.Title,
                    description = newBulletin.Content,
                    createTime = newBulletin.CreatedTime
                }
            }));
        }
Ejemplo n.º 17
0
        public JsonResult UpdateBulletin(BulletinRequest request)
        {
            var checkUsername = CheckUsername(request.username);

            if (checkUsername != null)
            {
                return(checkUsername);
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid bulletinId."));
            }

            if (!CornerCaseCheckHelper.Check(request.bulletinName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(402, "Invalid bulletinName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(403, "Invalid Description."));
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(401, "Invalid projectId."));
            }

            //Check if target bulletin exists
            var bulletin = _meshContext.Bulletins.Find(request.bulletinId);
            var project  = _meshContext.Projects.Find(request.projectId);
            var user     = _meshContext.Users.First(u => u.Email == request.username);

            if (bulletin == null || project == null)
            {
                return(JsonReturnHelper.ErrorReturn(420, "Invalid bulletinId or projectId"));
            }

            //Check if the user is admin of the project
            if (project.AdminId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(422, "Permission denied."));
            }

            try
            {
                bulletin.Title   = request.bulletinName;
                bulletin.Content = request.description;
                _meshContext.Bulletins.Update(bulletin);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(Json(new
            {
                err_code = 0,
                masg = "",
                data = new
                {
                    bulletinId = bulletin.Id,
                    bulletinName = bulletin.Title,
                    description = bulletin.Content,
                    createTime = bulletin.CreatedTime
                }
            }));
        }
Ejemplo n.º 18
0
        public JsonResult CreateTeamKB(MemoRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId"));
            }

            if (!CornerCaseCheckHelper.Check(request.knowledgeName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(802, "Invalid KnowledgeName."));
            }

            if (!CornerCaseCheckHelper.Check(request.hyperlink, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(803, "Invalid hyperlink."));
            }


            var user = _meshContext.Users.First(u => u.Email == request.username);
            var team = _meshContext.Teams.FirstOrDefault(t => t.Id == request.teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Team does not exist."));
            }

            var memoCollection = _meshContext.TeamMemoCollections.First(p => p.TeamId == team.Id);

            if (_permissionCheck.CheckTeamPermission(request.username, team) == PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(801, "Permission denied."));
            }

            var newMemo = new TeamMemo()
            {
                Title        = request.knowledgeName,
                CollectionId = memoCollection.Id,
                Text         = request.hyperlink,
                UserId       = user.Id
            };

            try
            {
                _meshContext.TeamMemos.Add(newMemo);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(MemoResult(new MemoInfo()
            {
                KnowledgeName = request.knowledgeName,
                HyperLink = request.hyperlink,
                CreateTime = newMemo.CreatedTime,
                KnowledgeId = newMemo.Id,
                UploaderName = request.username
            }));
        }
Ejemplo n.º 19
0
        public JsonResult QueryTeamTasks(string username, int teamId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId."));
            }


            var user = _meshContext.Users.First(u => u.Email == username);

            var team = _meshContext.Teams.FirstOrDefault(t => t.Id == teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Invalid teamId."));
            }

            if (_permissionCheck.CheckTeamPermission(username, team) == PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var tasks = _meshContext.Develops
                        .Where(d => d.UserId == user.Id)
                        .Join(_meshContext.TaskBoards, d => d.ProjectId, t => t.ProjectId, (d, t) => t)
                        .Join(_meshContext.Projects, t => t.ProjectId, p => p.Id, (t, p) => new
            {
                projectId      = p.Id,
                projectAdminId = p.AdminId,
                boardId        = t.Id
            })
                        .Join(_meshContext.Tasks, t => t.boardId, s => s.BoardId, (t, s) => new
            {
                t.projectAdminId,
                s
            })
                        .Join(_meshContext.Users, t => t.projectAdminId, u => u.Id, (t, u) => new
            {
                Founder = u.Nickname,
                task    = t.s
            })
                        .Select(m => new TaskInfo()
            {
                Id          = m.task.Id,
                CreatedTime = m.task.CreatedTime,
                Description = m.task.Description,
                EndTime     = m.task.EndTime,
                Founder     = m.Founder,
                Name        = m.task.Name,
                isFinished  = m.task.Finished,
                Principal   = _meshContext.Users.First(u => u.Id == m.task.LeaderId).Nickname,
            })
                        .ToList();

            foreach (var task in tasks)
            {
                task.SubTasks = _meshContext.Subtasks
                                .Where(b => b.TaskId == task.Id)
                                .Select(s => new SubTaskInfo()
                {
                    Title       = s.Title,
                    TaskId      = s.TaskId,
                    CreatedTime = s.CreatedTime,
                    Description = s.Description,
                    Founder     = task.Founder,
                    Status      = GetStatus(task.EndTime, s.Finished),
                    Principal   = _meshContext.Assigns
                                  .Where(a => a.TaskId == s.TaskId && a.Title == s.Title)
                                  .Join(_meshContext.Users, n => n.UserId, u => u.Id, (n, u) => u.Nickname)
                                  .ToList()
                })
                                .ToList();
            }

            return(TaskListResult(tasks));
        }
Ejemplo n.º 20
0
        public JsonResult QueryProjectTask(string username, int projectId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Invalid projectId."));
            }

            if (_permissionCheck.CheckProjectPermission(username, project) == PermissionCheckHelper.ProjectOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var founder = _meshContext.Users.First(u => u.Id == project.AdminId);

            var board = _meshContext.TaskBoards.First(b => b.ProjectId == projectId);
            var tasks = _meshContext.Tasks
                        .Where(s => s.BoardId == board.Id)
                        .Select(s => new TaskInfo()
            {
                Id          = s.Id,
                CreatedTime = s.CreatedTime,
                Description = s.Description,
                EndTime     = s.EndTime,
                Founder     = founder.Nickname,
                isFinished  = s.Finished,
                Name        = s.Name,
                Principal   = _meshContext.Users.First(u => u.Id == s.LeaderId).Nickname,
                Status      = GetStatus(s.EndTime, s.Finished),
                SubTasks    = _meshContext.Subtasks
                              .Where(b => b.TaskId == s.Id)
                              .Select(e => new SubTaskInfo()
                {
                    Title       = e.Title,
                    TaskId      = e.TaskId,
                    CreatedTime = e.CreatedTime,
                    Description = e.Description,
                    Status      = GetStatus(s.EndTime, e.Finished),
                    isFinished  = e.Finished,
                    Founder     = founder.Nickname,
                    Principal   = _meshContext.Assigns
                                  .Where(a => a.TaskId == e.TaskId && a.Title == e.Title)
                                  .Join(_meshContext.Users, n => n.UserId, u => u.Id, (n, u) => u.Nickname)
                                  .ToList()
                })
                              .ToList()
            })
                        .ToList();

            return(TaskListResult(tasks));
        }
Ejemplo n.º 21
0
        public JsonResult UpdateSubTask(SubTaskRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            if (!CornerCaseCheckHelper.Check(request.taskId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(501, "Invalid taskId."));
            }

            if (!CornerCaseCheckHelper.Check(request.subTaskName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(511, "Invalid subTaskName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(602, "Invalid description."));
            }

            if (!CornerCaseCheckHelper.Check(request.principal, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(101, "Invalid principal."));
            }


            var user = _meshContext.Users.First(u => u.Email == request.username);

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Project does not exist."));
            }

            var task = _meshContext.Tasks.FirstOrDefault(t => t.Id == request.taskId);

            if (task == null)
            {
                return(JsonReturnHelper.ErrorReturn(604, "Task does not exist."));
            }

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin &&
                task.LeaderId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var principalUser = _meshContext.Users.FirstOrDefault(u => u.Email == request.principal);

            if (principalUser == null || _permissionCheck.CheckProjectPermission(request.principal, project) ==
                PermissionCheckHelper.ProjectOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(608, "Principal does not exist."));
            }

            var subTask = _meshContext.Subtasks.FirstOrDefault(s => s.TaskId == request.taskId && s.Title == request.subTaskName);

            if (subTask == null)
            {
                return(JsonReturnHelper.ErrorReturn(606, "SubTask does not exist."));
            }

            var assign = _meshContext.Assigns.First(a => a.TaskId == subTask.TaskId && a.Title == subTask.Title);

            try
            {
                subTask.Finished    = request.isFinished;
                subTask.Description = request.description;
                _meshContext.Update(subTask);
                _meshContext.Assigns.Remove(assign);
                _meshContext.Assigns.Add(new Assign()
                {
                    UserId = principalUser.Id,
                    TaskId = subTask.TaskId,
                    Title  = subTask.Title
                });
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }


            return(SubTaskResult(new SubTaskInfo()
            {
                CreatedTime = subTask.CreatedTime,
                Description = subTask.Description,
                Founder = user.Nickname,
                TaskId = subTask.TaskId,
                Title = subTask.Title,
                isFinished = subTask.Finished,
                Status = GetStatus(task.EndTime, subTask.Finished),
                Principal = GetSubTaskPrincipals(subTask.TaskId, subTask.Title)
            }
                                 ));
        }
Ejemplo n.º 22
0
        public JsonResult UpdateTask(TaskRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            if (!CornerCaseCheckHelper.Check(request.taskId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(501, "Invalid taskId."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(602, "Invalid description."));
            }

            if (!CornerCaseCheckHelper.Check(request.principal, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(101, "Invalid principal."));
            }

            if (!CornerCaseCheckHelper.Check(request.deadline, 0, CornerCaseCheckHelper.Time))
            {
                return(JsonReturnHelper.ErrorReturn(610, "Invalid deadline."));
            }


            var user = _meshContext.Users.First(u => u.Email == request.username);

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Project does not exist."));
            }

            var task = _meshContext.Tasks.FirstOrDefault(t => t.Id == request.taskId);

            if (task == null)
            {
                return(JsonReturnHelper.ErrorReturn(604, "Task does not exist."));
            }

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin &&
                task.LeaderId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var principalUser = _meshContext.Users.FirstOrDefault(u => u.Email == request.principal);

            if (principalUser == null || _permissionCheck.CheckProjectPermission(request.principal, project) ==
                PermissionCheckHelper.ProjectOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(608, "Principal does not exist."));
            }

            var endTime = Convert.ToDateTime(request.deadline);

            try
            {
                task.Finished    = request.isFinished;
                task.Priority    = request.priority;
                task.EndTime     = endTime;
                task.Description = request.description;
                task.LeaderId    = principalUser.Id;
                _meshContext.Tasks.Update(task);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            var founder = _meshContext.Users.First(u => u.Id == project.AdminId);

            return(TaskResult(new TaskInfo()
            {
                Id = task.Id,
                CreatedTime = task.CreatedTime,
                Description = task.Description,
                EndTime = task.EndTime,
                Founder = founder.Nickname,
                Name = task.Name,
                isFinished = task.Finished,
                Status = GetStatus(task.EndTime, task.Finished),
                Principal = principalUser.Nickname,
                SubTasks = GetSubTasks(task.Id, founder.Nickname)
            }));
        }
Ejemplo n.º 23
0
        public JsonResult CreateTask(TaskRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            if (!CornerCaseCheckHelper.Check(request.taskName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(601, "Invalid taskName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(602, "Invalid description."));
            }

            if (!CornerCaseCheckHelper.Check(request.principal, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(101, "Invalid principal."));
            }

            if (!CornerCaseCheckHelper.Check(request.deadline, 0, CornerCaseCheckHelper.Time))
            {
                return(JsonReturnHelper.ErrorReturn(610, "Invalid deadline."));
            }

            var user = _meshContext.Users.First(u => u.Email == request.username);

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Project does not exist."));
            }

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var taskBoard = _meshContext.TaskBoards.First(b => b.ProjectId == request.projectId);

            var principalUser = _meshContext.Users.FirstOrDefault(u => u.Email == request.principal);

            if (principalUser == null)
            {
                return(JsonReturnHelper.ErrorReturn(603, "Principal does not exist."));
            }

            var endTime = Convert.ToDateTime(request.deadline);


            var task = new Task()
            {
                BoardId     = taskBoard.Id,
                Description = request.description,
                LeaderId    = principalUser.Id,
                Name        = request.taskName,
                Priority    = request.priority,
                EndTime     = endTime,
                StartTime   = DateTime.Now
            };

            //Start Transaction to save the task
            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Tasks.Add(task);
                    _meshContext.SaveChanges();
                    _meshContext.TaskFeeds.Add(new TaskFeed()
                    {
                        UserId = principalUser.Id,
                        TaskId = task.Id
                    });
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }

            return(TaskResult(new TaskInfo()
            {
                Name = task.Name,
                CreatedTime = task.CreatedTime,
                Description = task.Description,
                EndTime = task.EndTime,
                Founder = user.Nickname,
                Id = task.Id,
                isFinished = task.Finished,
                Principal = principalUser.Nickname,
                Status = GetStatus(task.EndTime, task.Finished),
                SubTasks = new List <SubTaskInfo>()
            }));
        }
        public JsonResult QueryUserInfo(string username, string keyword)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(keyword, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(130, "Invalid keyword."));
            }

            var usersAccordingToUsername = _meshContext.Users
                                           .Where(u => u.Email.Contains(keyword));
            var usersAccordingToNickname = _meshContext.Users
                                           .Where(u => u.Nickname.Contains(keyword));
            var usersAccordingToDescription = _meshContext.Users
                                              .Where(u => u.Description.Contains(keyword));
            var usersUnion = usersAccordingToUsername
                             .Union(usersAccordingToNickname)
                             .Union(usersAccordingToDescription)
                             .ToList();

            var users = new List <UserInfo>();

            foreach (var u in usersUnion)
            {
                var cooperation = _meshContext.Cooperations
                                  .Where(c => c.UserId == u.Id)
                                  .OrderByDescending(c => c.AccessCount)
                                  .FirstOrDefault();
                var teams = _meshContext.Cooperations
                            .Where(c => c.UserId == u.Id)
                            .Join(_meshContext.Teams, c => c.TeamId, t => t.Id, (c, t) => new TeamInfo()
                {
                    TeamId     = t.Id,
                    TeamName   = t.Name,
                    AdminId    = t.AdminId,
                    AdminName  = _meshContext.Users.First(s => s.Id == t.AdminId).Nickname,
                    CreateTIme = t.CreatedTime.ToString()
                })
                            .ToList();
                users.Add(new UserInfo()
                {
                    username    = u.Email,
                    address     = u.Address,
                    avatar      = AvatarSaveHelper.GetObject(u.Avatar),
                    birthday    = u.Birthday.ToString(),
                    description = u.Description,
                    preference  = new UserPreference()
                    {
                        preferenceColor    = u.ColorPreference,
                        preferenceLayout   = u.LayoutPreference,
                        preferenceShowMode = u.RevealedPreference,
                        preferenceTeam     = cooperation?.TeamId ?? -1
                    },
                    teams = teams
                });
            }

            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    isSuccess = true,
                    msg = "",
                    users = users
                }
            }));
        }
Ejemplo n.º 25
0
        public JsonResult DeleteNotification(string username, int type, int id)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(id, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(910, "Invalid Id"));
            }
            var user = _meshContext.Users.First(u => u.Email == username);

            switch (type)
            {
            case BULLETIN:
            {
                var bulletin =
                    _meshContext.BulletinFeeds.FirstOrDefault(f => f.BulletinId == id && f.UserId == user.Id);
                if (bulletin == null)
                {
                    return(JsonReturnHelper.ErrorReturn(901, "Invalid bulletinId"));
                }

                try
                {
                    _meshContext.BulletinFeeds.Remove(bulletin);
                    _meshContext.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }

                return(JsonReturnHelper.SuccessReturn());
            }

            case TASK:
            {
                var task = _meshContext.TaskFeeds.FirstOrDefault(f => f.TaskId == id && f.UserId == user.Id);
                if (task == null)
                {
                    return(JsonReturnHelper.ErrorReturn(902, "Invalid TaskId"));
                }

                try
                {
                    _meshContext.TaskFeeds.Remove(task);
                    _meshContext.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }

                return(JsonReturnHelper.SuccessReturn());
            }

            default:
                return(JsonReturnHelper.ErrorReturn(900, "Invalid type."));
            }
        }
Ejemplo n.º 26
0
        public JsonResult QueryTeam(string username, int teamId)
        {
            var checkResult = CheckUsername(username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId"));
            }

            var user = _meshContext.Users.First(u => u.Email == username);
            var team = _meshContext.Teams.FirstOrDefault(t => t.Id == teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Team does not exist."));
            }

            if (_permissionCheck.CheckTeamPermission(username, team) == PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(305, "Permission denied."));
            }

            //Find team members
            var teamCooperation = _meshContext.Cooperations
                                  .Where(c => c.TeamId == team.Id);
            var adminName = _meshContext.Users.First(u => u.Id == team.AdminId).Nickname;
            var members   = _meshContext.Users
                            .Join(teamCooperation, u => u.Id, c => c.UserId, (u, c) =>
                                  new Member()
            {
                Id       = u.Id,
                Username = u.Email,
                Nickname = u.Nickname,
                Avatar   = AvatarSaveHelper.GetObject(u.Avatar)
            }).ToList();

            //Find projects of the team
            var project = _meshContext.Projects
                          .Where(p => p.TeamId == teamId);
            var teamProjects = _meshContext.Users
                               .Join(project, u => u.Id, p => p.AdminId, (u, p) =>
                                     new TeamProject()
            {
                ProjectId   = p.Id,
                ProjectName = p.Name,
                AdminName   = u.Nickname,
                ProjectLogo = AvatarSaveHelper.GetObject(p.Icon)
            }).ToList();

            var userTeamCooperation = teamCooperation.First(c => c.UserId == user.Id);

            try
            {
                userTeamCooperation.AccessCount += 1;
                _meshContext.Cooperations.Update(userTeamCooperation);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    isSuccess = true,
                    msg = "",
                    team = new
                    {
                        teamId = team.Id,
                        teamName = team.Name,
                        createTime = team.CreatedTime,
                        adminName = adminName,
                        members = members,
                        teamProjects = teamProjects
                    }
                }
            }));
        }
Ejemplo n.º 27
0
        public JsonResult CreateTeam(CreateRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.teamName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(310, "Invalid teamName."));
            }

            var user        = _meshContext.Users.First(u => u.Email == request.username);
            var createdTeam = new Team()
            {
                Name    = request.teamName,
                AdminId = user.Id
            };

            //Start transaction to save the new team
            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Teams.Add(createdTeam);
                    _meshContext.SaveChanges();
                    _meshContext.Cooperations.Add(new Cooperation()
                    {
                        TeamId = createdTeam.Id,
                        UserId = user.Id
                    });
                    _meshContext.TeamMemoCollections.Add(new TeamMemoCollection()
                    {
                        TeamId = createdTeam.Id
                    });
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }

            //Return team members
            var teamMembers = new List <Member>();

            teamMembers.Add(new Member()
            {
                Username = user.Email, Id = user.Id
            });
            return(Json(new
            {
                err_code = 0,
                data = new
                {
                    isSuccess = true,
                    team = new
                    {
                        teamId = createdTeam.Id,
                        createTime = createdTeam.CreatedTime,
                        teamName = createdTeam.Name,
                        adminName = user.Nickname,
                        members = teamMembers
                    }
                }
            }
                        ));
        }
Ejemplo n.º 28
0
        public JsonResult CreateSubTask(SubTaskRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }

            if (!CornerCaseCheckHelper.Check(request.taskId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(501, "Invalid taskId."));
            }

            if (!CornerCaseCheckHelper.Check(request.subTaskName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(511, "Invalid subTaskName."));
            }

            if (!CornerCaseCheckHelper.Check(request.description, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(602, "Invalid description."));
            }

            if (!CornerCaseCheckHelper.Check(request.principal, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(101, "Invalid principal."));
            }

            var user = _meshContext.Users.First(u => u.Email == request.username);

            var project = _meshContext.Projects.FirstOrDefault(p => p.Id == request.projectId);

            if (project == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Project does not exist."));
            }

            var task = _meshContext.Tasks.FirstOrDefault(t => t.Id == request.taskId);

            if (task == null)
            {
                return(JsonReturnHelper.ErrorReturn(604, "Task does not exist."));
            }

            if (_permissionCheck.CheckProjectPermission(request.username, project) != PermissionCheckHelper.ProjectAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var subTask = _meshContext.Subtasks.FirstOrDefault(s => s.TaskId == task.Id && s.Title == request.subTaskName);

            if (subTask != null)
            {
                return(JsonReturnHelper.ErrorReturn(608, "subTaskName already exists."));
            }

            var principalUser = _meshContext.Users.FirstOrDefault(u => u.Email == request.principal);

            if (principalUser == null)
            {
                return(JsonReturnHelper.ErrorReturn(603, "Principal does not exist."));
            }

            var newSubTask = new Subtask()
            {
                TaskId      = task.Id,
                Title       = request.subTaskName,
                Description = request.description
            };

            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Subtasks.Add(newSubTask);
                    _meshContext.Assigns.Add(new Assign()
                    {
                        TaskId = newSubTask.TaskId,
                        Title  = newSubTask.Title,
                        UserId = principalUser.Id
                    });
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }


            return(SubTaskResult(new SubTaskInfo()
            {
                TaskId = newSubTask.TaskId,
                CreatedTime = newSubTask.CreatedTime,
                Description = newSubTask.Description,
                Founder = user.Nickname,
                Title = newSubTask.Title,
                isFinished = newSubTask.Finished,
                Status = GetStatus(task.EndTime, newSubTask.Finished),
                Principal = GetSubTaskPrincipals(newSubTask.TaskId, newSubTask.Title)
            }));
        }
Ejemplo n.º 29
0
        public JsonResult UpdateTeamKB(MemoRequest request)
        {
            var checkResult = CheckUsername(request.username);

            if (checkResult != null)
            {
                return(checkResult);
            }

            if (!CornerCaseCheckHelper.Check(request.teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId"));
            }

            if (!CornerCaseCheckHelper.Check(request.knowledgeName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(802, "Invalid KnowledgeName."));
            }

            if (!CornerCaseCheckHelper.Check(request.hyperlink, 100, CornerCaseCheckHelper.Description))
            {
                return(JsonReturnHelper.ErrorReturn(803, "Invalid hyperlink."));
            }
            var team = _meshContext.Teams.FirstOrDefault(p => p.Id == request.teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(707, "Invalid teamId."));
            }

            var memoCollection = _meshContext.TeamMemoCollections.First(p => p.TeamId == request.teamId);
            var knowledge      =
                _meshContext.TeamMemos.FirstOrDefault(
                    m => m.Id == request.knowledgeId && m.CollectionId == memoCollection.Id);

            if (knowledge == null)
            {
                return(JsonReturnHelper.ErrorReturn(805, "Invalid knowledgeId."));
            }

            var user = _meshContext.Users.First(u => u.Email == request.username);

            if (_permissionCheck.CheckTeamPermission(request.username, team) != PermissionCheckHelper.TeamAdmin ||
                knowledge.UserId != user.Id)
            {
                return(JsonReturnHelper.ErrorReturn(801, "Permission denied."));
            }

            var uploader = _meshContext.Users.First(u => u.Id == knowledge.UserId).Nickname;

            try
            {
                knowledge.Title = request.knowledgeName;
                knowledge.Text  = request.hyperlink;
                _meshContext.TeamMemos.Update(knowledge);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(MemoResult(new MemoInfo()
            {
                CreateTime = knowledge.CreatedTime,
                HyperLink = knowledge.Text,
                KnowledgeId = knowledge.Id,
                KnowledgeName = knowledge.Title,
                UploaderName = uploader
            }));
        }
Ejemplo n.º 30
0
        public JsonResult InviteNewTeamMember(InviteRequest request)
        {
            var checkUsernameResult = CheckUsername(request.username);

            if (checkUsernameResult != null)
            {
                return(checkUsernameResult);
            }

            if (!CornerCaseCheckHelper.Check(request.inviteName, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(108, "Invalid inviteName"));
            }

            if (!CornerCaseCheckHelper.Check(request.teamId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(301, "Invalid teamId"));
            }

            var team = _meshContext.Teams.FirstOrDefault(t => t.Id == request.teamId);

            if (team == null)
            {
                return(JsonReturnHelper.ErrorReturn(302, "Team not exist."));
            }

            if (_permissionCheck.CheckTeamPermission(request.username, team) != PermissionCheckHelper.TeamAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(305, "Permission denied."));
            }

            var inviteUser = _meshContext.Users.FirstOrDefault(u => u.Email == request.inviteName);

            if (inviteUser == null)
            {
                return(JsonReturnHelper.ErrorReturn(108, "Username or inviteName not exists."));
            }

            if (_permissionCheck.CheckTeamPermission(request.inviteName, team) != PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(306, "User already in team."));
            }

            var cooperation = new Cooperation()
            {
                TeamId = request.teamId,
                UserId = inviteUser.Id
            };

            try
            {
                _meshContext.Cooperations.Add(cooperation);
                _meshContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
            }

            return(JsonReturnHelper.SuccessReturn());
        }