Ejemplo n.º 1
0
        public IActionResult Post([FromBody] NotebookShareViewModel notebookShareViewModel)
        {
            var currentUser = User.Claims.Single(c => c.Type == ClaimTypes.Email).Value;

            var notebook = _context.Notebook.SingleOrDefault(n => n.Id == notebookShareViewModel.NotebookId);

            if (notebook == null)
            {
                return(NotFound(new { message = "Notebook not found" }));
            }

            if (!_notebookService.IsUserOwner(notebookShareViewModel.NotebookId, currentUser))
            {
                return(Forbid());
            }

            if (!_context.User.Any(u => u.Email == notebookShareViewModel.User))
            {
                return(NotFound(new { message = "User does not exist" }));
            }

            if (notebook.User == notebookShareViewModel.User)
            {
                return(BadRequest(new { message = "You can't add the owner in the share list" }));
            }

            if (_context.NotebookShare.Any(ns => ns.User == notebookShareViewModel.User && ns.NotebookId == notebookShareViewModel.NotebookId))
            {
                return(BadRequest(new { message = "The user is already in the share list" }));
            }

            if (ModelState.IsValid)
            {
                _context.NotebookShare.Add(new NotebookShare
                {
                    NotebookId = notebookShareViewModel.NotebookId,
                    User       = notebookShareViewModel.User,
                    CanEdit    = notebookShareViewModel.CanEdit,
                    DateShared = DateTime.Now
                });

                _context.NotebookCollection.Add(new NotebookCollection
                {
                    NotebookId = notebookShareViewModel.NotebookId,
                    User       = notebookShareViewModel.User
                });

                _context.SaveChanges();

                var notebookShare = _context.NotebookShare.Single(ns => ns.NotebookId == notebookShareViewModel.NotebookId && ns.User == notebookShareViewModel.User);

                return(Ok(notebookShare));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Put([FromBody] NotebookShareViewModel notebookShareViewModel)
        {
            var currentUser = User.Claims.Single(c => c.Type == ClaimTypes.Email).Value;

            if (!_notebookService.IsUserOwner(notebookShareViewModel.NotebookId, currentUser))
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                var notebookShare = _context.NotebookShare.Single(ns => ns.NotebookId == notebookShareViewModel.NotebookId && ns.User == notebookShareViewModel.User);

                notebookShare.CanEdit = notebookShareViewModel.CanEdit;

                _context.SaveChanges();

                return(Ok(notebookShare));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }