Exemplo n.º 1
0
        public IActionResult Put([FromBody] NotebookRateViewModel notebookRateViewModel)
        {
            var currentUser = User.Claims.Single(c => c.Type == ClaimTypes.Email).Value;

            if (!_notebookService.CanUserView(notebookRateViewModel.NotebookId, currentUser))
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                var notebookRate = _context.NotebookRate.SingleOrDefault(nr => nr.NotebookId == notebookRateViewModel.NotebookId && nr.User == currentUser);

                if (notebookRate == null)
                {
                    return(NotFound());
                }
                else
                {
                    notebookRate.Rate = notebookRateViewModel.Rate;

                    _context.SaveChanges();

                    return(Ok(notebookRate));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Exemplo n.º 2
0
        public IActionResult Post(NotebookRateViewModel notebookRateViewModel)
        {
            var currentUser = User.Claims.Single(c => c.Type == ClaimTypes.Email).Value;

            if (!_notebookService.CanUserView(notebookRateViewModel.NotebookId, currentUser))
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                _context.NotebookRate.Add(new NotebookRate
                {
                    NotebookId = notebookRateViewModel.NotebookId,
                    User       = currentUser,
                    Rate       = notebookRateViewModel.Rate
                });

                _context.SaveChanges();

                var notebookRate = _context.NotebookRate.Single(nr => nr.NotebookId == notebookRateViewModel.NotebookId && nr.User == currentUser);

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