public async Task <string> Handle(GenerateInviteToken request, CancellationToken cancellationToken) { var pendingInvite = await _context.TenantInvites.SingleOrDefaultAsync(ti => ti.TenantId == request.TenantId, cancellationToken : cancellationToken); if (pendingInvite != null) { if (pendingInvite.Expiration > DateTime.UtcNow) { return(pendingInvite.Token); } _context.TenantInvites.Remove(pendingInvite); await _context.SaveChangesAsync(cancellationToken); } var result = _tokenService.TokenFromGuid(request.TenantId); _context.TenantInvites.Add(new TenantInvite { TenantId = request.TenantId, Token = result, Expiration = DateTime.UtcNow.AddHours(4) }); await _context.SaveChangesAsync(cancellationToken); return(result); }
public async Task <IActionResult> PutUser(long id, User user) { if (id != user.PeopleId) { return(BadRequest()); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Unit> Handle(AssignGenresToTitle request, CancellationToken cancellationToken) { var existingAssociations = _context.TitleGenres.Where(tg => tg.TitleId == request.TitleId).ToList(); List <Guid> associated = new List <Guid>(); foreach (var name in request.Genres.Distinct()) { var genre = await _mediator.Send(new RegisterGenre { Name = name }, cancellationToken); if (_context.TitleGenres .SingleOrDefault(tg => tg.TitleId == request.TitleId && tg.GenreId == genre) == null) { _context.Add(new TitleGenre { TitleId = request.TitleId, GenreId = genre }); await _context.SaveChangesAsync(cancellationToken); } associated.Add(genre); } await RemoveUnusedAssociations(cancellationToken, existingAssociations, associated); return(Unit.Value); }
public async Task <IActionResult> Create([Bind("Id,Name")] Game game) { if (ModelState.IsValid) { _context.Add(game); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(game)); }
public async Task <IActionResult> Create([Bind("Id,GameId,DatePlayed")] GamePlayed gamePlayed) { if (ModelState.IsValid) { _context.Add(gamePlayed); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["GameId"] = new SelectList(_context.Games, "Id", "Name", gamePlayed.GameId); return(View(gamePlayed)); }
public async Task <ActionResult <UserGame> > PutUser(long id, UserGamePutRequest request) { var userGame = await _context.UserGame.FindAsync(id); if (userGame == null) { return(NotFound()); } userGame.IsWish = request.IsWish; await _context.SaveChangesAsync(); return(userGame); }
public async Task <GameViewModel> Handle(RegisterGame request, CancellationToken cancellationToken) { var exists = _context.PlatformGames.SingleOrDefault(g => g.Code == request.Code); if (exists == null) { var game = RegisterGame(request); var platformGame = RegisterGameWithPlatform(request, game); await _context.SaveChangesAsync(cancellationToken); return(new GameViewModel { Id = game.Id, Name = game.Name, Description = game.Description, Code = request.Code, Registered = platformGame.Registered, Platform = _context.Platforms.Single(p => p.Id == request.Platform).Name }); } return(null); }
public async Task <TitleViewModel> Handle(RegisterTitle request, CancellationToken cancellationToken) { var title = _context.Titles .SingleOrDefault(x => x.Name == request.Name && x.Subtitle == request.Subtitle && x.TenantId == request.TenantId); if (title == null) { title = new Title { Name = request.Name, Subtitle = request.Subtitle, TenantId = request.TenantId }; _context.Titles.Add(title); await _context.SaveChangesAsync(cancellationToken); } return(new TitleViewModel { Id = title.Id, Name = title.Name, Subtitle = title.Subtitle }); }
public async Task <GameViewModel> Handle(UpdateGame request, CancellationToken cancellationToken) { var result = await(from pg in _context.PlatformGames join g in _context.Games on pg.GameId equals g.Id where pg.Id == request.Id select new { Game = g, PlatformInfo = pg }).SingleOrDefaultAsync(cancellationToken); if (result != null) { result.Game.Name = request.Name; result.Game.Description = request.Description; _context.Update(result.Game); await _context.SaveChangesAsync(cancellationToken); return(new GameViewModel { Id = request.Id, Name = result.Game.Name, Description = result.Game.Description, Code = result.PlatformInfo.Code, Registered = result.PlatformInfo.Registered }); } return(null); }
public async Task <RegisterUserResult> Handle(RegisterUser request, CancellationToken cancellationToken) { var existing = _context.Users.SingleOrDefault(u => u.Email == request.Email); if (existing != null) { return(new RegisterUserResult { TenantId = existing.TenantId, UserId = existing.Id }); } var tenantId = GetTenantIdFromInvite(request.Token); var user = new User { Email = request.Email, TenantId = tenantId ?? Guid.NewGuid() }; _context.Users.Add(user); await _context.SaveChangesAsync(cancellationToken); return(new RegisterUserResult { TenantId = user.TenantId, UserId = user.Id }); }
public async Task <TitleViewModel> Handle(UpdateTitle request, CancellationToken cancellationToken) { var existing = _context.Titles.Single(t => t.Id == request.TitleId); if (!string.IsNullOrEmpty(request.Name)) { existing.Name = request.Name; existing.Subtitle = request.Subtitle; await _context.SaveChangesAsync(cancellationToken); } if (request.Genres != null) { await _mediator.Send(new AssignGenresToTitle { TitleId = existing.Id, Genres = request.Genres }, cancellationToken); } return(new TitleViewModel { Id = existing.Id, Name = existing.Name, Subtitle = existing.Subtitle }); }
public async Task <Unit> Handle(AttachImageToGame request, CancellationToken cancellationToken) { if (request.Content.Length > 0) { var gameImage = new GameImage { Id = Guid.NewGuid(), FileName = request.FileName, GameId = request.GameId }; if (_settings.Local) { if (!Directory.Exists(_imagePath)) { Directory.CreateDirectory(_imagePath); } File.WriteAllBytes(Path.Combine(_imagePath, $"{request.GameId}_{gameImage.Id}.jpg"), request.Content); } else { await _mediator.Send(new ResizeImagesAndStore { ImageContent = request.Content, GameId = request.GameId, ImageId = gameImage.Id }, cancellationToken); } _context.Add(gameImage); await _context.SaveChangesAsync(cancellationToken); } return(Unit.Value); }
private async Task <Game> RegisterGame(RegisterGame request, TitleViewModel title) { var game = _context.Games.SingleOrDefault(g => g.TenantId == request.TenantId && ( (!string.IsNullOrEmpty(request.Code) && g.Code == request.Code) || g.PlatformId == request.Platform && g.TitleId == title.Id )); if (game != null) { return(game); } game = new Game() { TitleId = title.Id, PlatformId = request.Platform, Registered = DateTime.UtcNow, Code = request.Code, TenantId = request.TenantId }; _context.Games.Add(game); await _context.SaveChangesAsync(); return(game); }
public async Task <IActionResult> Edit(int id, [Bind("Id,KeyCode,Type")] GameResultType gameResultType) { if (id != gameResultType.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(gameResultType); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GameResultTypeExists(gameResultType.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(gameResultType)); }
public async Task <Unit> Handle(FavoriteGame request, CancellationToken cancellationToken) { var game = await _context.Games.SingleAsync(g => g.Id == request.GameId, cancellationToken); game.IsFavorite = true; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <CopyViewModel> Handle(UpdateCopy request, CancellationToken cancellationToken) { var copy = _context.Copies.Single(cp => cp.Id == request.Id); copy.Tags = JsonSerializer.Serialize(request.Tags); copy.IsWanted = request.IsWanted; copy.Purchased = request.Purchased; copy.Cost = request.Cost; await _context.SaveChangesAsync(cancellationToken); return(null); }
public async Task <Guid?> Handle(AddCopy request, CancellationToken cancellationToken) { var copy = new Copy { GameId = request.GameId, Cost = request.Cost, TenantId = request.TenantId, Tags = JsonSerializer.Serialize(request.Tags), Purchased = request.Purchased, IsWanted = request.IsWanted }; _context.Copies.Add(copy); await _context.SaveChangesAsync(cancellationToken); return(copy.Id); }
public async Task <Unit> Handle(AttachImageToGame request, CancellationToken cancellationToken) { if (!Directory.Exists(_imagePath)) { Directory.CreateDirectory(_imagePath); } if (request.Content.Length > 0) { await using var fileStream = new FileStream(Path.Combine(_imagePath, request.FileName.PrepareFileName()), FileMode.Create); await request.Content.CopyToAsync(fileStream, cancellationToken); _context.Add(new PlatformGameImage { FileName = request.FileName.PrepareFileName(), PlatformGameId = request.Id }); await _context.SaveChangesAsync(cancellationToken); } return(Unit.Value); }