public async Task <ActionResult <ShelfDto> > PutShelfAsync(long id, PostData data) { var uid = User?.GetNumericId(); if (uid is null) { return(Unauthorized("Not logged in")); } var shelf = await _context.Shelves.FindAsync(id); // Check existence if (shelf == null) { return(NotFound()); } // Check ownership if (shelf.OwnerId != uid) { return(Unauthorized("Not owner")); } shelf.Name = data.Name; shelf.Description = data.Description; shelf.Color = data.Color; shelf.IsPublic = data.IsPublic; shelf.TrackUpdates = data.TrackUpdates; shelf.IsQuickAdd = data.IsQuick; shelf.IconId = data.Icon; await _context.SaveChangesAsync(); return(Ok(ShelfMappings.ToShelfDto().Compile().Invoke(shelf))); }
public async Task<ActionResult<IEnumerable<ShelfDto>>> GetUserShelvesAsync(string name) { var uid = User?.GetNumericId(); var shelves = await _context.Shelves .Where(s => s.Owner.NormalizedUserName == name.Normalize().ToUpperInvariant()) .Where(s => s.OwnerId == uid || s.IsPublic) .Select(ShelfMappings.ToShelfDto()) .ToListAsync(); return shelves.Count > 0 ? Ok(shelves) : NoContent(); }
public async Task<ActionResult<IEnumerable<ShelfDto>>> GetCurrentUserShelvesAsync(long story) { var uid = User?.GetNumericId(); if (uid is null) return Unauthorized(); var shelves = await _context.Shelves .Where(s => s.Owner.Id == uid) .Select(ShelfMappings.ToShelfDto(story)) .AsNoTracking() .ToListAsync(); return shelves.Count > 0 ? Ok(shelves) : NoContent(); }