/// <summary> /// Add the entity /// </summary> /// <param name="model"><see cref="T"/></param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="DbException"></exception> /// <exception cref="Exception"></exception> /// <returns><see cref="bool"/>Returns the model</returns> public virtual async Task Add(T model) { if (model == null) { throw new ArgumentNullException(nameof(model), "Cannot add a null reference"); } try { await _context.Set <T>().AddAsync(model); await _context.SaveChangesAsync(); } catch (DbException ex) { ex.Data["params"] = new List <object> { model }; throw; } catch (Exception ex) { ex.Data["params"] = new List <object> { model }; throw; } }
public async Task <ActionResult> Logout() { var username = User.GetClaim(ClaimTypes.Name); await _service.Unsubscribe(User.GetClaim(ClaimTypes.NameIdentifier)); TwitchChatTask.DisconnectUser(username); // Create event token for this user using var db = new TwitchBotContext(); var eventToken = await db.EventTokens.FirstOrDefaultAsync(x => x.Username == username); if (eventToken != null) { db.Remove(eventToken); await db.SaveChangesAsync(); } return(Ok()); }
public async Task <ActionResult <LoginResponse> > Login([FromBody] LoginRequest request) { if (string.IsNullOrEmpty(request?.Username)) { return(Unauthorized()); } if (!_settings.Logins.TryGetValue(request.Username, out var password) || request.Password != password) { _logger.LogWarning("Failed login for " + request.Username); return(Unauthorized()); } // Create subscription for user var(user, subscriptions) = await _service.Subscribe(request.Username); if (user == null) { return(NotFound()); } // Connect to twitch chat TwitchChatTask.WatchUser(user); // Create event token for this user using var db = new TwitchBotContext(); var eventToken = await db.EventTokens.FirstOrDefaultAsync(x => x.Username == request.Username.ToLower()); if (eventToken == null) { eventToken = new(request.Username.ToLower(), user.UserId); await db.AddAsync(eventToken); await db.SaveChangesAsync(); } var response = new LoginResponse(eventToken.Token, subscriptions, new(user.DisplayName, user.UserId, user.ImageUrl)); return(Ok(response)); }