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
            }));
        }
        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
                }
            }));
        }
Example #3
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 DeleteProject(string username, int teamId, int projectId)
        {
            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(projectId, 0, CornerCaseCheckHelper.Id))
            {
                return(JsonReturnHelper.ErrorReturn(701, "Invalid projectId."));
            }


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

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

            //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 in the team
            var teamCheckResult = _permissionCheck.CheckTeamPermission(username, team);

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

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

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

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

            return(JsonReturnHelper.SuccessReturn());
        }