示例#1
0
        public ActionResult CreateBoard(CreateBoardViewModel model, int projectId)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView(model));
            }

            string userId = User.Identity.GetUserId();
            GenericServiceResult result = boardService.CreateBoard(model, userId, projectId);

            if (result == GenericServiceResult.Error)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                ModelState.AddModelError("Error", "Unknown error");
                return(PartialView(model));
            }

            if (result == GenericServiceResult.AccessDenied)
            {
                Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return(RedirectToAction("Forbidden", "Error"));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        //This method simply creates empty CreateBoardViewModel
        public CreateBoardViewModel GetCreateBoardViewModel()
        {
            var viewModel = new CreateBoardViewModel()
            {
                CreateBoardFormDto = new CreateBoardFormDto()
            };

            return(viewModel);
        }
        public GenericServiceResult CreateBoard(CreateBoardViewModel model, string userId, int projectId)
        {
            try
            {
                bool isUserCanCreateBoard = CanUserCreateBoard(userId, projectId);

                if (!isUserCanCreateBoard)
                {
                    return(GenericServiceResult.AccessDenied);
                }

                ProjectUser projectUser = unitOfWork.ProjectUser.GetFirstOrDefaultWhere(p => p.ProjectId == projectId && p.UserId == userId);

                BoardSettings boardSettings = new BoardSettings
                {
                    AccessToChangeTask  = (int)model.AccessToChangeTask,
                    AccessToCreateTask  = (int)model.AccessToCreateTask,
                    AccessToDeleteTask  = (int)model.AccessToDeleteTask,
                    AccessToChangeBoard = (int)model.AccessToChangeBoard
                };

                Board board = new Board
                {
                    Title     = model.Title,
                    Settings  = boardSettings,
                    ProjectId = projectId,
                    CreatorId = userId
                };

                unitOfWork.Boards.Create(board);

                string creatorId    = unitOfWork.ProjectUser.GetFirstOrDefaultWhere(p => p.ProjectId == projectId && p.Role == (int)ProjectRoles.Administrator).UserId;
                string projectTitle = unitOfWork.Projects.Get(projectId).Title;

                NotificationMessage message = new NotificationMessage
                {
                    Title     = $"{projectTitle}",
                    Message   = $"Board {board.Title} was created",
                    Initiator = userId,
                    SendTo    = creatorId
                };
                Notifyer.Notify(message);

                return(GenericServiceResult.Success);
            }
            catch
            {
                return(GenericServiceResult.Error);
            }
        }
示例#4
0
        public async Task <IActionResult> Create([FromForm] CreateBoardViewModel createBoardViewModel, [FromRoute] int pageNumber)
        {
            TryValidateModel(createBoardViewModel);
            if (ModelState.IsValid)
            {
                Board board = new Board()
                {
                    UserName  = createBoardViewModel.UserName,
                    Subject   = createBoardViewModel.Subject,
                    Content   = createBoardViewModel.Content,
                    WriteDate = createBoardViewModel.WriteDate
                };
                mDbContext.Add(board);
                await mDbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index), "Boards", new { pageNumber }));
            }

            return(View(createBoardViewModel));
        }