/// <summary> /// Save <see cref="OzonProductPriceHistory"/> data into DB /// </summary> /// <param name="productHistoryRecord"></param> /// <returns></returns> public async Task <int> SaveProductHistoryAsync(OzonProductPriceHistory productHistoryRecord) { if (productHistoryRecord == null) { logger.Debug($"{nameof(productHistoryRecord)} is null!"); return(0); } using (var client = new SqliteContext(_connectionString)) { if (productHistoryRecord.Id == 0) { client.OzonProductPriceHistories.Add(productHistoryRecord); await client.SaveChangesAsync(); } else { client.Update(productHistoryRecord); await client.SaveChangesAsync(); } return(productHistoryRecord.Id); } }
public async Task<LocalIssue> CreateOrUpdateIssueAsync(LocalIssue issue) { using (context = new SqliteContext(BaseConnstr)) { var existed = context.Issue .AsNoTracking() .Include(i => i.Row) .Include(i => i.Column) .FirstOrDefault(iss => iss.Id == issue.Id); mapper.Map(issue, existed); if (existed == null) { var newiss = mapper.Map<SqliteIssue>(issue); context.Attach(newiss.Row); context.Attach(newiss.Column); await context.AddAsync(newiss); await context.SaveChangesAsync(); context.Update(newiss.Column); context.Update(newiss.Row); await context.SaveChangesAsync(); return mapper.Map<LocalIssue>(newiss); } context.Update(existed); await context.SaveChangesAsync(); return issue; } }
public async Task Clear(CommandContext ctx) { using (SqliteContext lite = new SqliteContext()) { var allProfiles = lite.Profiles; var queue = lite.playerQueue; foreach (var profile in allProfiles) { profile.role = null; profile.queue = false; profile.queueSr = 0; await lite.SaveChangesAsync(); } foreach (var player in queue) { queue.Remove(player); await lite.SaveChangesAsync(); } dps = 0; tank = 0; support = 0; await ctx.Channel.SendMessageAsync("Queue has been cleared."); } }
public async Task SoftBan(CommandContext ctx, DiscordMember user, TimeSpan banLength, [RemainingText] string banReason) { using (SqliteContext lite = new SqliteContext()) { var straightJacket = ctx.Guild.Roles.Values.First(x => x.Name == "Straight Jacket"); var bans = lite.Bans; ulong userID = user.Id; foreach (var member in bans) { if (member.userID == userID) { member.unbanTime += banLength; await ctx.Channel.SendMessageAsync($"User already temp banned. Added {banLength} to their ban time."); await lite.SaveChangesAsync(); return; } } var userBan = new Ban(); userBan.userID = user.Id; userBan.userName = user.Username; userBan.banTime = DateTime.Now; userBan.unbanTime = userBan.banTime + banLength; userBan.banReason = banReason; bans.Add(userBan); await lite.SaveChangesAsync(); await user.GrantRoleAsync(straightJacket); await ctx.Channel.SendMessageAsync($"{user.Username} has been temp banned until {userBan.unbanTime.ToString("HH:mm MM/dd/yyyy")}"); } }
public async Task Simp(CommandContext ctx, DiscordMember recipient, int amount) { using (SqliteContext lite = new SqliteContext()) { var user = await GetOrCreateProfileAsync(lite, ctx.Member, ctx.Guild.Id); var recipientProfile = await GetOrCreateProfileAsync(lite, recipient, ctx.Guild.Id); if (amount <= 0) { await ctx.Channel.SendMessageAsync($"Invalid amount given."); return; } else if (user.DripScore < amount) { await ctx.Channel.SendMessageAsync($"**{ctx.Member.Username}**, you do not have enough drip to give that amount."); return; } user.DripScore -= amount; recipientProfile.DripScore += amount; await lite.SaveChangesAsync(); var urls = new string[] { "https://cdn.discordapp.com/attachments/799182855899971626/802711276798738442/blush1.gif", "https://media.giphy.com/media/PR3wumHIdsBhu/giphy.gif", "https://media.giphy.com/media/HPI9m7McNPGN2/giphy.gif", "https://media.giphy.com/media/vIIbrC80HHN7O/giphy.gif", "https://media.giphy.com/media/UrPxdGW62TDtS/giphy.gif", "https://media.giphy.com/media/19jcWuNGzaI5W/giphy.gif", "https://media.giphy.com/media/6o9roFSbffy9O/giphy.gif", "https://media.giphy.com/media/l1KdaAqHtx1ojBI2c/giphy.gif" }; int randUrl = new Random().Next(0, urls.Length); var url = urls.ElementAt(randUrl); var simpEmbed = new DiscordEmbedBuilder { Title = $"{ctx.Member.Username} has simped for {recipientProfile.Username} :heart:", Color = DiscordColor.Rose, Description = $"**{ctx.Member.Username}**, you have donated **{amount}** drip to **{recipientProfile.Username}**.", }; simpEmbed.WithImageUrl(url); var r = new Random(); if (r.Next(0, 4) == 3 && (amount >= 3)) { user.DripCooldown = DateTime.Now; simpEmbed.AddField("**--KARMA BONUS--**", "Cooldown for **m!drip** has been reset. nice!"); await lite.SaveChangesAsync(); } await ctx.Channel.SendMessageAsync(embed : simpEmbed); } }
public async Task <T> Insert <T>(T entity) where T : Model { _context.Lock.WaitOne(); await _context.Set <T>().AddAsync(entity); await _context.SaveChangesAsync(); _context.Lock.ReleaseMutex(); return(entity); }
public async Task <IHttpActionResult> CreateEventData(AnimalType animalType) { if (animalType != null && !ModelState.IsValid) { return(BadRequest(ModelState)); } _sqliteContext.AnimalTypeEntities.Add(animalType); await _sqliteContext.SaveChangesAsync(); return(Created(animalType)); }
public async Task <IHttpActionResult> CreateEventData(EventData eventData) { if (eventData != null && !ModelState.IsValid) { return(BadRequest(ModelState)); } _sqliteContext.EventDataEntities.Add(eventData); await _sqliteContext.SaveChangesAsync(); return(Created(eventData)); }
public async Task RefreshQuota(CommandContext ctx) { using (SqliteContext lite = new SqliteContext()) { var userProfile = GetOrCreateProfileAsync(lite, ctx.Member, ctx.Guild.Id).Result; var currentDate = DateTime.Now; if (userProfile.GambleCooldown < currentDate) { if (userProfile.GambleCount == 0) { await ctx.Channel.SendMessageAsync($"**{userProfile.Username}**, you have not used any tickets yet."); return; } userProfile.GambleCount = 0; await ctx.Channel.SendMessageAsync($"**{userProfile.Username}**'s your daily quota has been reset"); userProfile.GambleCooldown = DateTime.Now.AddDays(1); await lite.SaveChangesAsync(); } else { var coolDownTimespan = userProfile.GambleCooldown - currentDate; await ctx.Channel.SendMessageAsync($"**{ctx.Member.DisplayName}**, you can refresh your daily quota in " + $"**{coolDownTimespan.Hours}h {coolDownTimespan.Minutes} mins**"); } } }
public async Task RemoveDB(CommandContext ctx) { using (SqliteContext lite = new SqliteContext()) { try { /*if (targetUser != null) * { * foreach (var item in lite.Profile) * { * if (item.MemberId == targetUser.Id) * { * lite.Profile.Remove(item); * } * } * await ctx.Channel.SendMessageAsync($"successfully removed all entities from SqliteDB.db for **{targetUser.Username}**"); * return; * } * else * {*/ foreach (var item in lite.Profile) { lite.Profile.Remove(item); } await ctx.Channel.SendMessageAsync("successfully removed all entities from SqliteDB.db"); await lite.SaveChangesAsync(); } catch (Exception e) { await ctx.Channel.SendMessageAsync(e.ToString()); } } }
public async Task ChangeRoleIfNecessary_TheRoleWillNotDuplicate_OkAsync() { await using var context = new SqliteContext(); var user = await new ApplicationUserFactory(Role.Employee).BuildAsync(context); user = await context.Users .IncludeRoles() .ByIdOrNullAsync(user.Id); Assert.NotNull(user); Assert.Single(user.Roles); Assert.Equal(Role.Employee, (Role)user.Roles.First()); user.ChangeRoleIfNecessary(Role.SystemAdministrator); await context.SaveChangesAsync(); user = await context.Users .IncludeRoles() .ByIdOrNullAsync(user.Id); Assert.Single(user.Roles); Assert.Equal(Role.SystemAdministrator, (Role)user.Roles.First()); Assert.Equal(1, await context.UserRoles.CountAsync()); }
public async Task <Profile> GetOrCreateProfileAsync(SqliteContext lite, DiscordMember user, ulong guildId) { var recordedUser = await lite.Profile .Where(x => x.guildId == guildId) .FirstOrDefaultAsync(x => x.MemberId == user.Id); if (recordedUser != null) { return(recordedUser); } recordedUser = new Profile { MemberId = user.Id, guildId = guildId, Username = user.Username, UserIconURL = user.AvatarUrl, DripScore = 100, GambleTickets = 5, DripInventory = "", }; lite.Add(recordedUser); await lite.SaveChangesAsync(); return(recordedUser); }
static void Main(string[] args) { var dt = new DataTable(); var x1 = dt.Compute("(200-12.2)/5", "false"); Console.WriteLine(x1.ToString()); //Dictionary<string, Func<int, int>> dic = new Dictionary<string, Func<int, int>>(); //dic.Add("+", AddCalc); //var dd = dic.ContainsKey("-") ? dic["-"](3) : 0; //Console.WriteLine(dic["-"](3).ToString()); DbContextOptions <SqliteContext> dbContext = new DbContextOptions <SqliteContext>(); //DbContextOptionsBuilder<SqliteContext> optionsBuilder = new DbContextOptionsBuilder<SqliteContext>(); //optionsBuilder.UseSqlite(@"E:\Code\VS2017\ProjectPractice\ProjectPractice.Sqlite\bin\Debug\lab.db"); //DbContext db = new DbContext(); SqliteContext db = new SqliteContext(dbContext); UserInfo userInfo = new UserInfo { Id = new Guid().ToString(), Name = "zjh12345" }; db.UserInfos.Add(userInfo); db.Database.EnsureCreatedAsync(); db.SaveChangesAsync(); //typeof(Program).Assembly.GetTypes().ToList().ForEach(m => Debug.WriteLine(m.Name)); //Blog blog = new Blog {BlogId = 1, Url = "www.163.com" }; //SqliteContext sqliteContext = new SqliteContext(); //sqliteContext.Database.EnsureCreatedAsync(); ////sqliteContext.Add(blog); //sqliteContext.Add(userInfo); //sqliteContext.SaveChanges(); }
public async Task ProfileCreation(CommandContext ctx, int dps, int tank, int support) { using (SqliteContext lite = new SqliteContext()) { var allProfiles = lite.Profiles; ulong author = ctx.Message.Author.Id; if (dps > 5000 || dps < 0 || tank > 5000 || tank < 0 || support > 5000 || support < 0) { await ctx.Channel.SendMessageAsync("Invalid sr."); return; } foreach (var _profile in allProfiles) { if (_profile.userID == author) { _profile.dps = dps; _profile.tank = tank; _profile.support = support; _profile.userName = ctx.Message.Author.Username; _profile.queue = false; await lite.SaveChangesAsync(); await ctx.Channel.SendMessageAsync("Your profile has been updated."); return; } } var profile = new Profile(); profile.userID = ctx.Message.Author.Id; profile.userName = ctx.Message.Author.Username; profile.dps = dps; profile.tank = tank; profile.support = support; profile.queue = false; lite.Profiles.Add(profile); await lite.SaveChangesAsync(); await ctx.Channel.SendMessageAsync("Added your profile."); } }
public async Task <IActionResult> Put([FromBody] Product product) { if (product == null || product.Id == 0 || String.IsNullOrEmpty(product.ProductName)) { return(BadRequest()); } var oldProduct = _context.Products.SingleOrDefault(p => p.Id == product.Id); if (oldProduct == null) { return(NotFound()); } _context.Entry(oldProduct).CurrentValues.SetValues(product); await _context.SaveChangesAsync(); return(Ok(product)); }
public async Task DeleteIssueAsync(int?issueId) { using (context = new SqliteContext(BaseConnstr)) { context.Issue .Remove(context.Issue.Find(issueId)); await context.SaveChangesAsync(); } }
public async Task Dps(CommandContext ctx, int dps) { using (SqliteContext lite = new SqliteContext()) { var allProfiles = lite.Profiles; ulong author = ctx.Message.Author.Id; if (dps > 5000 || dps < 0) { await ctx.Channel.SendMessageAsync("Invalid sr."); return; } foreach (var _profile in allProfiles) { if (_profile.userID == author) { _profile.dps = dps; _profile.userName = ctx.Message.Author.Username; _profile.queue = false; await lite.SaveChangesAsync(); await ctx.Channel.SendMessageAsync("DPS sr updated."); return; } } var profile = new Profile(); profile.userID = ctx.Message.Author.Id; profile.userName = ctx.Message.Author.Username; profile.dps = dps; profile.queue = false; lite.Profiles.Add(profile); await lite.SaveChangesAsync(); await ctx.Channel.SendMessageAsync("Added your profile with only a DPS value."); } }
public async Task DeleteColumnAsync(int?columnId) { using (context = new SqliteContext(BaseConnstr)) { if (context.Column.First().Id == columnId) { var bindedIssues = context.Issue .Where(iss => iss.ColumnId == columnId); var newxtCol = context.Column.Skip(1).First(); foreach (var issue in bindedIssues) { issue.ColumnId = newxtCol.Id; } context.UpdateRange(bindedIssues); await context.SaveChangesAsync(); context.Column.Remove(context.Column.Find(columnId)); await context.SaveChangesAsync(); var t = context.Issue.ToList(); } else { var bindedIssues = context.Issue .Where(iss => iss.ColumnId == columnId); var previousCol = context.Column.LastOrDefault(r => r.Id < columnId); foreach (var issue in bindedIssues) { issue.ColumnId = previousCol.Id; } context.UpdateRange(bindedIssues); context.Column.Remove(context.Column.Find(columnId)); await context.SaveChangesAsync(); var t = context.Issue.ToList(); } } }
public async Task BuyTicket(CommandContext ctx, int wantedAmount = 0) { using (SqliteContext lite = new SqliteContext()) { var userProfile = await GetOrCreateProfileAsync(lite, ctx.Member, ctx.Guild.Id); var dripCost = wantedAmount * 5; if (wantedAmount <= 0) { return; } else if (wantedAmount > 99) { await ctx.Channel.SendMessageAsync($"**{userProfile.Username}**, you can't purchase more than **99** tickets at once."); return; } else if (userProfile.DripScore < dripCost) { await ctx.Channel.SendMessageAsync($"**{userProfile.Username}**, you don't have enough drip to purchase **{wantedAmount}** ticket(s). " + $"\n(tickets cost **5** drip each)"); return; } var buyEmbed = new DiscordEmbedBuilder { Color = DiscordColor.Blurple }; buyEmbed.AddField($"{userProfile.Username}, spend {dripCost} drip :fire: to purchase {wantedAmount} Tickets :tickets:?" , $"please respond with **\"y\"** to confirm your purchase:"); //check if user confirms purchase var interactivity = ctx.Client.GetInteractivity(); await ctx.Channel.SendMessageAsync(embed : buyEmbed); var confirmation = await interactivity.WaitForMessageAsync(x => x.Channel == ctx.Channel && x.Author.Id == ctx.Member.Id); if (confirmation.Result.Content != "y" || confirmation.TimedOut) { await ctx.Channel.SendMessageAsync("Purchase Cancelled."); return; } await ctx.Channel.SendMessageAsync($"**Success**, you now have **{wantedAmount}** tickets(s) :tickets:"); userProfile.DripScore -= dripCost; userProfile.GambleTickets += wantedAmount; await lite.SaveChangesAsync(); } }
public async Task<RowInfo> CreateOrUpdateRowAsync(RowInfo row) { using (context = new SqliteContext(BaseConnstr)) { if (row.Id == 0 || context.Row.Find(row.Id) == null) await context.AddAsync(row); else context.Update(row); await context.SaveChangesAsync(); return row; } }
public async Task <ActionResult <Book> > AddBook(string id) { if (BookExists(id)) { return(BadRequest()); } BookModel bookModel = await _googleBooksService.GetGoogleBookById(id); if (bookModel == null) { return(NotFound()); } Book book = _mapper.Map <Book>(bookModel); _context.Book.Add(book); await _context.SaveChangesAsync(); return(book); }
public async Task<ColumnInfo> CreateOrUpdateColumnAsync(ColumnInfo column) { using (context = new SqliteContext(BaseConnstr)) { if (column.Id == 0 || context.Column.AsNoTracking() .FirstOrDefault(c => c.Id == column.Id) == null) await context.AddAsync(column); else context.Update(column); await context.SaveChangesAsync(); return column; } }
public async Task DeleteRowAsync(int? rowId) { using (context = new SqliteContext(BaseConnstr)) { if (context.Row.First().Id == rowId) { var bindedIssues = context.Issue .Where(iss => iss.RowId == rowId); var newxtRow = context.Row.Skip(1).First(); foreach (var issue in bindedIssues) issue.RowId = newxtRow.Id; context.UpdateRange(bindedIssues); await context.SaveChangesAsync(); context.Row.Remove(context.Row.Find(rowId)); await context.SaveChangesAsync(); var t = context.Issue.ToList(); } else { var bindedIssues = context.Issue .Where(iss => iss.RowId == rowId); var previousRow = context.Row.LastOrDefault(r => r.Id < rowId); foreach (var issue in bindedIssues) issue.RowId = previousRow.Id; context.UpdateRange(bindedIssues); context.Row.Remove(context.Row.Find(rowId)); await context.SaveChangesAsync(); var t = context.Issue.ToList(); } } }
public async Task <TestEntity> Create(string title, string desc) { using var db = new SqliteContext(); var entity = new TestEntity { Title = title, Description = desc, }; await db.Tests.AddAsync(entity); await db.SaveChangesAsync(); return(entity); }
public async Task Leave(CommandContext ctx) { using (SqliteContext lite = new SqliteContext()) { var author = ctx.Message.Author.Id; var allProfiles = lite.Profiles; var queue = lite.playerQueue; foreach (var player in queue) { if (player.userID == author) { foreach (var profile in allProfiles) { if (profile.userID == author) { if (player.role == "dps") { dps--; } if (player.role == "tank") { tank--; } if (player.role == "support") { support--; } profile.role = null; profile.queue = false; profile.queueSr = 0; queue.Remove(player); await lite.SaveChangesAsync(); await ctx.Channel.SendMessageAsync("You have left the queue"); return; } } } } await ctx.Channel.SendMessageAsync($"{ ctx.User.Mention} you are not in queue"); } }
public async Task <List <Art> > UpdateArt() { var art = await GetAllArt(); using (var db = new SqliteContext()) { await db.Database.ExecuteSqlRawAsync("DELETE FROM `Arts`"); art.ForEach(async x => { await db.Arts.AddAsync(x.Adapt <ArtEntity>()); }); await db.SaveChangesAsync(); } return(art); }
public static async Task <DiscordGuild> SyncTableAsync(SqliteContext sqliteContext, SocketGuild socketGuild) { var discordGuild = await sqliteContext.DiscordGuildTable.Where(a => a.GuildId == socketGuild.Id).FirstOrDefaultAsync().ConfigureAwait(false); if (discordGuild != null) { return(discordGuild); } discordGuild = new DiscordGuild { GuildId = socketGuild.Id }; sqliteContext.DiscordGuildTable.Add(discordGuild); await sqliteContext.SaveChangesAsync().ConfigureAwait(false); return(discordGuild); }
public async Task <BoardInfo> CreateOrUpdateBoardInfoAsync(BoardInfo info) { using (context = new SqliteContext(BaseConnstr)) { if (info.Id == 0 || context.Board.AsNoTracking() .FirstOrDefault(c => c.Id == info.Id) == null) { await context.AddAsync(info); } else { context.Update(info); } await context.SaveChangesAsync(); return(info); } }
public async Task <RowInfo> CreateOrUpdateRowAsync(RowInfo row) { using (context = new SqliteContext(BaseConnstr)) { if (row.Id == 0 || context.Row.AsNoTracking() .FirstOrDefault(c => c.Id == row.Id) == null) { context.Attach(row.Board); await context.AddAsync(row); } else { context.Update(row); } await context.SaveChangesAsync(); return(row); } }
public async Task RemoveBan(CommandContext ctx, DiscordMember user, [RemainingText] string reason) { using (SqliteContext lite = new SqliteContext()) { var userID = user.Id; var bans = lite.Bans; var straightJacket = ctx.Guild.Roles.Values.First(x => x.Name == "Straight Jacket"); foreach (var ban in bans) { if (ban.userID == userID) { bans.Remove(ban); await lite.SaveChangesAsync(); await user.RevokeRoleAsync(straightJacket); await ctx.Channel.SendMessageAsync($"Ban has been removed from {user.Username}."); } } } }