Ejemplo n.º 1
0
        public async Task <IActionResult> GetById([FromBody] FunficByIdRequestModel getRequestModel)
        {
            var funfic = _appDbContext.Funfics
                         .Include(f => f.Chapters)
                         .Include(f => f.Tags)
                         .Join(_appDbContext.Users,
                               f => f.UserId,
                               u => u.Id,
                               (f, u) => new FunficModel
            {
                Id               = f.Id,
                Author           = u.UserName,
                Name             = f.Name,
                Genre            = f.Genre,
                ShortDescription = f.ShortDescription,
                Tags             = f.Tags.Select(t => t.Name),
                Rating           = f.Rating,
                ScoreCount       = f.ScoreCount,
                CreatedAt        = f.CreatedAt,
                Chapters         = f.Chapters.Select(chap => _mapper.Map <PartialChapter>(chap))
            })
                         .Single(f => f.Id == getRequestModel.Id);

            if (funfic == null)
            {
                return(StatusCode(404, "No such funfic."));
            }
            return(StatusCode(200, funfic));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Delete([FromBody] FunficByIdRequestModel deleteRequestModel)
        {
            var role = User.Claims
                       .FirstOrDefault(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");

            var existingFunfic = _appDbContext.Funfics
                                 .FirstOrDefault(f => f.Id == deleteRequestModel.Id);

            if (existingFunfic == null)
            {
                return(StatusCode(404, "No such funfic existing"));
            }

            var owner = await _userManager.FindByNameAsync(existingFunfic.UserId);

            if (role.Value != "admin" && User.Identity.Name != owner.UserName)
            {
                return(StatusCode(403));
            }

            _appDbContext.Funfics.Remove(existingFunfic);

            try
            {
                await _appDbContext.SaveChangesAsync();
            }
            catch
            {
                return(StatusCode(409));
            }

            return(StatusCode(200));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetByUserId([FromBody] FunficByIdRequestModel getRequestModel)
        {
            var role = User.Claims
                       .FirstOrDefault(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");

            var funfics = _appDbContext.Funfics
                          .Include(f => f.Chapters)
                          .Include(f => f.Tags)
                          .Where(f => f.UserId == getRequestModel.Id)
                          .Join(_appDbContext.Users,
                                f => f.UserId,
                                u => u.Id,
                                (f, u) => new PartialFunficModel
            {
                Id               = f.Id,
                Author           = u.UserName,
                Name             = f.Name,
                Genre            = f.Genre,
                ShortDescription = f.ShortDescription,
                Tags             = f.Tags.Select(t => t.Name),
                Rating           = f.Rating,
                ScoreCount       = f.ScoreCount,
                CreatedAt        = f.CreatedAt
            });

            if (funfics == null)
            {
                return(StatusCode(404, "No funfics"));
            }
            return(StatusCode(200, funfics));
        }