예제 #1
0
        public async Task <IActionResult> ApproveBook([FromBody] PostApproveModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Teacher" }))
                {
                    Teacher         current = this.GetLoggedUser(_auth, _context) as Teacher;
                    AppIdentityUser author  = await _auth.FindUserById(model.PostAuthorId);

                    if (ModelState.IsValid)
                    {
                        Book book = await _context.GetBook(model.PostId);

                        if (book != null && book.IsApproved == false)
                        {
                            book.IsApproved = true;
                            _context.Update(book);

                            await _context.Add(new Notifier(_context, _auth).Approved(book, current));

                            if (await _context.SaveAll())
                            {
                                author.Point += 15;
                                await _auth.UpdateUser(author);

                                _logger.LogApproveBook(book, current.Email, Request.Path);
                                return(Ok(new SuccesApproveModel(current)));
                            }
                            return(BadRequest("Error approving book"));
                        }
                        return(NotFound("Book not found or is already approved"));
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
예제 #2
0
        public async Task <IActionResult> DisapproveBook([FromBody] PostApproveModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Teacher" }))
                {
                    Teacher         current = this.GetLoggedUser(_auth, _context) as Teacher;
                    AppIdentityUser author  = await _auth.FindUserById(model.PostAuthorId);

                    if (ModelState.IsValid)
                    {
                        Book book = await _context.GetByIdAsync <Book>(x => x.Id == model.PostId);

                        if (book != null && book.IsApproved == false)
                        {
                            _context.Delete(book);

                            await _context.Add(new Notifier(_context, _auth).Disapproved(book, current, model.Reason));

                            if (await _context.SaveAll())
                            {
                                _logger.LogDisapproveBook(book, current.Email, Request.Path, model.Reason);
                                return(Ok($"The book has been deleted by {current.Name} {current.Surname}"));
                            }
                            return(BadRequest("Error disapproving book"));
                        }
                        return(NotFound("Book not found or is already approved"));
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }