Exemple #1
0
        public async Task <ActionResult> Item(string id)
        {
            var item = await _itemDbCommand.FindAsync(id);

            if (item == null)
            {
                return(HttpNotFound());
            }

            var model = Mapper.Map <ItemViewModel>(item);

            model.CanEdit               = LogonUser.IsEntitledToEditItem(item);
            model.CanDelete             = LogonUser.IsEntitledToDeleteItem(item);
            model.CanEditCollarborators = LogonUser.IsEntitledToEditItemCollaborators(item);
            model.CanWriteComments      = LogonUser.IsEntitledToWriteComments(item);

            ViewBag.Title = model.DisplayTitle;

            var comments = await _commentDbCommand.GetByItemAsync(item);

            foreach (var comment in comments)
            {
                var commentModel = Mapper.Map <CommentViewModel>(comment);
                if (comment.User == LogonUser)
                {
                    commentModel.IsCommentAuthor = true;
                }
                model.Comments.Add(commentModel);
            }

            model.NewComment = (LogonUser == null) ? new CommentEditModel() : Mapper.Map <CommentEditModel>(item.NewComment(LogonUser));

            return(View("Item", model));
        }
Exemple #2
0
        public async Task <ActionResult> UpdateCollaboratorRole(string id, string userId, RoleType type)
        {
            var item = await _itemDbCommand.FindAsync(id);

            if (item == null)
            {
                return(HttpNotFound());
            }
            if (!LogonUser.IsEntitledToEditItemCollaborators(item))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            var targetCollaborator = item.Collaborators.FirstOrDefault(x => x.Id == userId);

            if (targetCollaborator == null)
            {
                throw new InvalidOperationException();
            }

            targetCollaborator.Role = type;

            await _itemDbCommand.SaveCollaboratorsAsync(item);

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Exemple #3
0
        public async Task <ActionResult> EditCollaborators(string id)
        {
            var item = await _itemDbCommand.FindAsync(id);

            if (item == null)
            {
                return(HttpNotFound());
            }
            if (!LogonUser.IsEntitledToEditItemCollaborators(item))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            var model = Mapper.Map <ItemEditCollaboratorsModel>(item);

            return(View(model));
        }
Exemple #4
0
        public async Task <ActionResult> RemoveCollaborator(string id, string userId)
        {
            var item = await _itemDbCommand.FindAsync(id);

            if (item == null)
            {
                return(HttpNotFound());
            }
            if (!LogonUser.IsEntitledToEditItemCollaborators(item))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            var deleteCollaborator = item.Collaborators.FirstOrDefault(x => x.Id == userId);

            if (deleteCollaborator == null)
            {
                throw new InvalidOperationException();
            }

            await _itemDbCommand.RemoveCollaboratorAsync(item, deleteCollaborator);

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Exemple #5
0
        public async Task <ActionResult> AddCollaborator(string id, string userId)
        {
            var item = await _itemDbCommand.FindAsync(id);

            if (item == null)
            {
                return(HttpNotFound());
            }
            if (!LogonUser.IsEntitledToEditItemCollaborators(item))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            var targetUser = await _userDbCommand.FindAsync(userId);

            if (targetUser == null)
            {
                return(HttpNotFound());
            }

            if (item.Collaborators.Contains(targetUser))
            {
                throw new InvalidOperationException();
            }

            var newCollaborator = new Collaborator(targetUser)
            {
                Role = RoleType.Member
            };

            await _itemDbCommand.AddCollaboratorAsync(item, newCollaborator);

            var model = Mapper.Map <CollaboratorEditModel>(newCollaborator);

            return(PartialView("_CollaboratorEdit", model));
        }