public JsonResult PreferenceColor(PreferenceRequest request)
        {
            var checkResult = CheckUsername(request.username);

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

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


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

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

            return(JsonReturnHelper.SuccessReturn());
        }
Ejemplo n.º 2
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.º 3
0
        public JsonResult CreateProjectKB(MemoRequest request)
        {
            var checkResult = CheckUsername(request.username);

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

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

            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 project = _meshContext.Projects.FirstOrDefault(p => p.Id == request.projectId);

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

            var memoCollection = _meshContext.ProjectMemoCollections.First(p => p.ProjectId == project.Id);

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

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

            try
            {
                _meshContext.ProjectMemos.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.º 4
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>()
            }));
        }
Ejemplo n.º 5
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.º 6
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.º 7
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.º 8
0
        public JsonResult CreateProject(ProjectRequest 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.ProjectName, 50, CornerCaseCheckHelper.Title))
            {
                return(JsonReturnHelper.ErrorReturn(710, "Invalid projectName."));
            }

            if (!CornerCaseCheckHelper.Check(request.AdminName, 50, CornerCaseCheckHelper.Username))
            {
                return(JsonReturnHelper.ErrorReturn(711, "Invalid adminName"));
            }


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

            //Check if admin exists
            var admin = _meshContext.Users.FirstOrDefault(a => a.Email == request.AdminName);

            if (admin == null)
            {
                return(JsonReturnHelper.ErrorReturn(704, "Admin does not exist."));
            }

            //Check if team exists
            var team = _meshContext.Teams.FirstOrDefault(t => t.Id == request.TeamId);

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

            //Check if admin is in the team
            var teamCheckResult = _permissionCheck.CheckTeamPermission(request.AdminName, team);

            if (teamCheckResult == PermissionCheckHelper.TeamOutsider)
            {
                return(JsonReturnHelper.ErrorReturn(702, "Invalid admin."));
            }

            //Check if user is the admin of the team
            teamCheckResult = _permissionCheck.CheckTeamPermission(request.Username, team);
            if (teamCheckResult != PermissionCheckHelper.TeamAdmin)
            {
                return(JsonReturnHelper.ErrorReturn(701, "Permission denied."));
            }

            var newProject = new Project()
            {
                Name      = request.ProjectName,
                AdminId   = admin.Id,
                TeamId    = team.Id,
                Publicity = request.IsPublic,
                Icon      = AvatarSaveHelper.PutObject("")
            };

            var members = new List <MemInfo> {
                new MemInfo()
                {
                    UserId = admin.Id, Username = admin.Email, Nickname = admin.Nickname, Avatar = AvatarSaveHelper.GetObject(admin.Avatar)
                }
            };

            //Start a transaction to save the project
            using (var transaction = _meshContext.Database.BeginTransaction())
            {
                try
                {
                    _meshContext.Projects.Add(newProject);
                    _meshContext.SaveChanges();
                    _meshContext.Develops.Add(new Develop()
                    {
                        ProjectId = newProject.Id,
                        UserId    = admin.Id
                    });
                    _meshContext.BulletinBoards.Add(new BulletinBoard()
                    {
                        ProjectId = newProject.Id
                    });
                    _meshContext.TaskBoards.Add(new TaskBoard()
                    {
                        ProjectId = newProject.Id
                    });
                    _meshContext.ProjectMemoCollections.Add(new ProjectMemoCollection()
                    {
                        ProjectId = newProject.Id
                    });
                    _meshContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    return(JsonReturnHelper.ErrorReturn(1, "Unexpected error."));
                }
            }

            return(ProjectResult(newProject, admin.Nickname));
        }