Пример #1
0
        [Authorize(AuthenticationSchemes = "AccessToken", Roles = "Admin")] // admin only action
        public async Task <ActionResult> UpdateSampleSylvreBlock(int id, SylvreBlockDto updatedSylvreBlock)
        {
            var sylvreBlockEntity = await _context.SylvreBlocks.SingleOrDefaultAsync(
                x => x.Id == id && x.IsSampleBlock);

            if (sylvreBlockEntity == null)
            {
                return(NotFound());
            }

            _context.SylvreBlocks.Attach(sylvreBlockEntity);

            if (!string.IsNullOrWhiteSpace(updatedSylvreBlock.Name))
            {
                sylvreBlockEntity.Name = updatedSylvreBlock.Name;
            }

            if (!string.IsNullOrWhiteSpace(updatedSylvreBlock.Body))
            {
                sylvreBlockEntity.Body = updatedSylvreBlock.Body;
            }

            _context.SylvreBlocks.Update(sylvreBlockEntity);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Пример #2
0
 private SylvreBlock GetSylvreBlockEntityFromDto(SylvreBlockDto dto, int userId)
 {
     return(new SylvreBlock
     {
         Name = dto.Name,
         Body = dto.Body,
         UserId = userId
     });
 }
Пример #3
0
        public async Task <ActionResult <SylvreBlockResponseDto> > CreateSylvreBlock(SylvreBlockDto newSylvreBlock)
        {
            int userId = int.Parse(User.Identity.Name);

            var entity = GetSylvreBlockEntityFromDto(newSylvreBlock, userId);

            _context.SylvreBlocks.Add(entity);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("CreateSylvreBlock", new { id = entity.Id },
                                   GetSylvreBlockResponseDtoFromEntity(entity)));
        }
Пример #4
0
        public async Task <ActionResult> UpdateSylvreBlock(int id, SylvreBlockDto updatedSylvreBlock)
        {
            var entity = await _context.SylvreBlocks.FindAsync(id);

            if (entity == null)
            {
                return(NotFound(new { Message = "SylvreBlock with given id not found" }));
            }

            // return only blocks that belong to the authenticated user
            int userId = int.Parse(User.Identity.Name);

            if (entity.UserId != userId)
            {
                return(Forbid("AccessToken"));
            }

            if (entity.IsSampleBlock)
            {
                return(BadRequest());
            }

            _context.SylvreBlocks.Attach(entity);

            if (!string.IsNullOrWhiteSpace(updatedSylvreBlock.Name))
            {
                entity.Name = updatedSylvreBlock.Name;
            }

            if (!string.IsNullOrWhiteSpace(updatedSylvreBlock.Body))
            {
                entity.Body = updatedSylvreBlock.Body;
            }

            _context.SylvreBlocks.Update(entity);
            await _context.SaveChangesAsync();

            return(NoContent());
        }