public async Task <TObject> AddAsync(TObject t) { DbContext.Set <TObject>().Add(t); await DbContext.SaveChangesAsync(); return(t); }
public async Task DeleteAsync(int pageAssociationId) { var pageAssociation = await _context.PageAssociations.SingleOrDefaultAsync(x => x.PageAssociationId == pageAssociationId); if (pageAssociation == null) { return; } if (pageAssociation.PageSection != null) { if (!await _context.PageAssociations.AnyAsync(x => x.PageSectionId == pageAssociation.PageSectionId)) { var pageSection = await _context.PageSections.SingleOrDefaultAsync(x => x.PageSectionId == pageAssociation.PageSectionId); _context.PageSections.Remove(pageSection); } } else if (pageAssociation.PagePartial != null) { if (!_context.PageAssociations.Any(x => x.PagePartialId == pageAssociation.PagePartialId)) { var pagePartial = await _context.PagePartials.SingleOrDefaultAsync(x => x.PagePartialId == pageAssociation.PagePartialId); _context.PagePartials.Remove(pagePartial); } } _context.PageAssociations.Remove(pageAssociation); await _context.SaveChangesAsync(); }
public async Task Create(FoodAddInfo info) { var model = _mapper.Map <FoodAddInfo, Food>(info); _db.Foods.Add(model); await _db.SaveChangesAsync(); }
public async Task <Class> Add(Class model) { var result = _db.Class.Add(model); await _db.SaveChangesAsync(); return(result.Entity); }
public async Task <IActionResult> Add(AddRepositoryViewModel dto) { if (!ModelState.IsValid) { return(View("Add", dto)); } if (await _dbContext.DockerRepositories.AnyAsync(x => x.Name == dto.Name.Trim())) { ModelState.AddModelError("Name", "名称已经存在"); } string registry = null; string schema = null; if (!string.IsNullOrWhiteSpace(dto.Registry)) { if (Uri.TryCreate(dto.Registry, UriKind.RelativeOrAbsolute, out var uri)) { schema = uri.Scheme; registry = $"{uri.Host}{(uri.Port == 80 || uri.Port == 443 ? "" : $":{uri.Port}")}"; } else { ModelState.AddModelError("Registry", "Registry 格式不正确"); } } if (await _dbContext.DockerRepositories.AnyAsync(x => x.Registry == dto.Registry || x.Repository == dto.Repository)) { ModelState.AddModelError("Registry", "镜像仓储已经存在"); } if (!ModelState.IsValid) { return(View("Add", dto)); } else { var repository = new DockerRepository { Name = dto.Name, Schema = schema, Registry = registry, Repository = dto.Repository, UserName = dto.UserName, Password = dto.Password, CreationTime = DateTime.Now }; _dbContext.DockerRepositories.Add(repository); await _dbContext.SaveChangesAsync(); return(Redirect("/docker-repository")); } }
public async Task <Student> Add(Student model) { var alreadyExists = await _db.Student.FirstOrDefaultAsync(s => s.ClassId == model.ClassId && s.Name == model.Name); if (alreadyExists != null) { throw new Exception("User already exists"); } var result = _db.Student.Add(model); await _db.SaveChangesAsync(); return(result.Entity); }
public async Task <int> AddAsync(string postCategoryName) { var newPostCategory = new PostCategory { PostCategoryName = postCategoryName }; _context.PostCategories.Add(newPostCategory); await _context.SaveChangesAsync(); return(newPostCategory.PostCategoryId); }
public async Task AddAsync(int postId, int imageId, PostImageType postImageType) { var postImage = new PostImage { PostId = postId, PostImageType = postImageType, ImageId = imageId }; _context.PostImages.Add(postImage); await _context.SaveChangesAsync(); }
public async Task <string> AddAsync(string roleName) { var newRole = new ApplicationRole { Name = roleName }; _context.Roles.Add(newRole); await _context.SaveChangesAsync(); return(newRole.Id); }
public async Task <int> UpsertAsync(int themeId, string themeName, int titleFontId, int textFontId, int largeTitleFontSize, int mediumTitleFontSize, int smallTitleFontSize, int tinyTitleFontSize, int textStandardFontSize, string pageBackgroundColour, string menuBackgroundColour, string menuTextColour) { var existingTheme = await GetAsync(themeId); if (existingTheme == null) { var newTheme = new CustomTheme { ThemeName = themeName, TitleFontId = titleFontId, TextFontId = textFontId, DateAdded = DateTime.Now, DateUpdated = DateTime.Now, TitleLargeFontSize = largeTitleFontSize, TitleMediumFontSize = mediumTitleFontSize, TitleSmallFontSize = smallTitleFontSize, TitleTinyFontSize = tinyTitleFontSize, TextStandardFontSize = textStandardFontSize, PageBackgroundColour = pageBackgroundColour, MenuBackgroundColour = menuBackgroundColour, MenuTextColour = menuTextColour, IsDefault = false, }; _context.Themes.Add(newTheme); await _context.SaveChangesAsync(); return(newTheme.ThemeId); } else { existingTheme.ThemeName = themeName; existingTheme.TitleFontId = titleFontId; existingTheme.TextFontId = textFontId; existingTheme.DateUpdated = DateTime.Now; existingTheme.TitleLargeFontSize = largeTitleFontSize; existingTheme.TitleMediumFontSize = mediumTitleFontSize; existingTheme.TitleSmallFontSize = smallTitleFontSize; existingTheme.TitleTinyFontSize = tinyTitleFontSize; existingTheme.TextStandardFontSize = textStandardFontSize; existingTheme.PageBackgroundColour = pageBackgroundColour; existingTheme.MenuBackgroundColour = menuBackgroundColour; existingTheme.MenuTextColour = menuTextColour; await _context.SaveChangesAsync(); return(existingTheme.ThemeId); } }
public async Task <int> CreateAsync(string imageFilePath, ImageCategory imageCategory) { var image = new Image { ImagePath = imageFilePath, ImageCategory = imageCategory }; _context.Images.Add(image); await _context.SaveChangesAsync(); return(image.ImageId); }
public async Task <IApiResult> AddAsync(AddRepositoryViewModel dto) { if (!ModelState.IsValid) { return(new FailedResult("Arguments invalid")); } if (await _dbContext.DockerRepositories.AnyAsync(x => x.Name == dto.Name.Trim())) { return(new FailedResult("名称已经存在")); } string registry = null; string schema = null; if (!string.IsNullOrWhiteSpace(dto.Registry)) { if (Uri.TryCreate(dto.Registry, UriKind.RelativeOrAbsolute, out var uri)) { schema = uri.Scheme; registry = $"{uri.Host}{(uri.Port == 80 || uri.Port == 443 ? "" : $":{uri.Port}")}"; } else { return(new FailedResult("Registry 格式不正确")); } } if (await _dbContext.DockerRepositories.AnyAsync(x => x.Registry == dto.Registry || x.Repository == dto.Repository)) { return(new FailedResult("镜像仓储已经存在")); } var repository = new DockerRepository { Name = dto.Name, Schema = schema, Registry = registry, Repository = dto.Repository, UserName = dto.UserName, Password = dto.Password, CreationTime = DateTimeOffset.Now }; _dbContext.DockerRepositories.Add(repository); await _dbContext.SaveChangesAsync(); return(new ApiResult("OK")); }
public async Task <IActionResult> Add(AddRepositoryViewModel dto) { if (!ModelState.IsValid) { return(View("Add", dto)); } var items = await _dbContext.DockerRepositories.Where(x => x.Name == dto.Name || x.Repository == dto.Repository).ToListAsync(); var valid = true; if (items.Any(x => x.Name == dto.Name)) { ModelState.AddModelError("Name", "名称已经存在"); valid = false; } if (items.Any(x => x.Repository == dto.Repository && x.Registry == dto.Registry)) { ModelState.AddModelError("Repository", "镜像仓储已经存在"); if (valid) { valid = false; } } if (!valid) { return(View("Add", dto)); } else { var repository = new DockerRepository { Name = dto.Name, Registry = string.IsNullOrWhiteSpace(dto.Registry) ? null : new Uri(dto.Registry).AbsoluteUri, Repository = dto.Repository, UserName = dto.UserName, Password = dto.Password, CreationTime = DateTime.Now }; _dbContext.DockerRepositories.Add(repository); await _dbContext.SaveChangesAsync(); return(Redirect("/docker-repository")); } }
public async Task <int> Handle(UserCreateCommand request, CancellationToken cancellationToken) { try { var user = new User() { Email = request.Email, FirstName = request.FirstName, IsEnable = true, LastName = request.LastName, Password = request.Password, Phone = request.Phone, UserName = request.UserName, TimeCreated = DateTime.Now }; _db.Users.Add(user); var result = await _db.SaveChangesAsync(); return(result); } catch (Exception) { throw; } }
public async Task <int> CreateAsync(string copyName, string copyBody) { var newCopy = new CopyItem { CopyName = copyName, CopyBody = copyBody, DateAdded = DateTime.Now, DateUpdated = DateTime.Now }; _context.CopyItems.Add(newCopy); await _context.SaveChangesAsync(); return(newCopy.CopyId); }
public async Task AddAsync(int pageId, string area, string controller, string action) { var page = await _context.Pages.SingleOrDefaultAsync(x => x.PageId == pageId); if (page == null) { return; } var orderPosition = 0; if (page.PageAssociations.Any()) { orderPosition = page.PageAssociations.Max(x => x.PageAssociationOrder + 1); } var pageAssociation = new PageAssociation { PageId = page.PageId, PageAssociationOrder = orderPosition, PagePartial = new PagePartial { RouteArea = area, RouteController = controller, RouteAction = action } }; _context.PageAssociations.Add(pageAssociation); await _context.SaveChangesAsync(); }
public async Task <int> Create(Food food) { var result = _db.Foods.Add(food); await _db.SaveChangesAsync(); return(result.Entity.Id); }
public async Task <IActionResult> LoginCallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { return(BadRequest($"Error from external provider: {remoteError}")); } var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(BadRequest()); } var user = await userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey); if (user != null) { return(await TokenResult(user)); } var email = info.Principal.FindFirstValue(ClaimTypes.Email); if (string.IsNullOrEmpty(email)) { email = info.Principal.FindFirstValue(ClaimTypes.Name); } // Lookup if there's an username with this e-mail address in the Db user = await userManager.FindByEmailAsync(email); if (user != null) { return(await TokenResult(user)); } // Create a unique username using the 'nameidentifier' claim var idKey = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"; var username = $"{info.LoginProvider}-{info.Principal.FindFirst(idKey).Value}"; user = new PortalUser { UserName = username, Email = email }; await userManager.CreateAsync(user); // Remove Lockout and E-Mail confirmation user.EmailConfirmed = true; user.LockoutEnabled = false; // Register this external provider to the user await userManager.AddLoginAsync(user, info); // Persist everything into the Db await appDbContext.SaveChangesAsync(); return(await TokenResult(user)); }
public async Task <int> CreateAsync(string fontName, string fontType, string fontFilePath) { var newFont = new Font { FontName = fontName, FontType = fontType, FontPath = fontFilePath, DateAdded = DateTime.Now, DateUpdated = DateTime.Now }; _context.Fonts.Add(newFont); await _context.SaveChangesAsync(); return(newFont.FontId); }
public async Task <int> Handle(CreateFoodCommand request, CancellationToken cancellationToken) { var food = _mapper.Map <CreateFoodCommand, Food>(request); var newFood = _db.Foods.Add(food); await _db.SaveChangesAsync(); return(newFood.Entity.Id); }
public async Task Process(TRequest request, TResponse response, CancellationToken cancellationToken) { if (request is ICommittableRequest) { await _db.SaveChangesAsync(); } //return await next(); }
public async Task <int> AddAsync(string pageName, string area, string controller, string action) { var newPage = new Page { PageName = pageName, PageArea = area, PageController = controller, PageAction = action, DateAdded = DateTime.Now, DateUpdated = DateTime.Now }; _context.Pages.Add(newPage); await _context.SaveChangesAsync(); return(newPage.PageId); }
public async Task LogPageViewAsync(string area, string controller, string action, string referredUrl, string ipAddress, string userAgent, string userId) { var newAnalyticPageView = new AnalyticPageView { Area = area ?? "", Controller = controller, Action = action, ReferredUrl = referredUrl, IPAddress = ipAddress, UserAgent = userAgent, UserId = userId ?? string.Empty, DateAdded = DateTime.Now }; _context.AnalyticPageViews.Add(newAnalyticPageView); await _context.SaveChangesAsync(); }
public async Task <bool> CreateAsync(SpiderViewObject vo) { if (ModelState.IsValid) { var exists = await _dbContext.Spiders.AnyAsync(x => x.Name == vo.Name); if (exists) { throw new ApplicationException($"Name {vo.Name} exists"); } try { TriggerBuilder.Create().WithCronSchedule(vo.Cron).Build(); } catch { throw new ApplicationException($"Cron {vo.Cron} is invalid"); } var spider = _mapper.Map <Data.Spider>(vo); spider.Enabled = true; spider.CreationTime = DateTimeOffset.Now; spider.LastModificationTime = DateTimeOffset.Now; await _dbContext.Spiders.AddAsync(spider); await _dbContext.SaveChangesAsync(); var id = spider.Id.ToString(); await ScheduleJobAsync(vo.Cron, id, spider.Name); return(true); } else { throw new ApplicationException("ModelState is invalid"); } }
public async Task <bool> CommitAsync() { try { await _db.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task <IActionResult> Add(AddRepositoryViewModel dto) { if (!ModelState.IsValid) { return(View("Add", dto)); } var items = await _dbContext.DockerRepositories.Where(x => x.Name == dto.Name || x.Repository == dto.Repository).ToListAsync(); if (items.Any(x => x.Name == dto.Name)) { ModelState.AddModelError("Name", "名称已经存在"); } if (items.Any(x => x.Repository == dto.Repository)) { ModelState.AddModelError("Repository", "镜像仓储已经存在"); } if (items.Any()) { return(View("Add", dto)); } else { _dbContext.DockerRepositories.Add(new DockerRepository { Name = dto.Name, Registry = dto.Registry, Repository = dto.Repository, CreationTime = DateTime.Now }); await _dbContext.SaveChangesAsync(); return(Redirect("/docker-repository")); } }
public async Task <int> CreateAsync(string postTitle, int postCategoryId, string postAuthorUserId, string postDescription, string postBody) { var post = new Post { PostTitle = postTitle, PostCategoryId = postCategoryId, PostAuthorUserId = postAuthorUserId, PostDescription = postDescription, PostBody = postBody, DateAdded = DateTime.Now, DateUpdated = DateTime.Now }; _context.Posts.Add(post); await _context.SaveChangesAsync(); return(post.PostId); }
public async Task Create(PostAddModel model) { var post = new Post { Body = model.Body, Content = model.Content, UserId = model.UserId, MimeType = model.MimeType, TimeCreated = DateTime.Now }; _db.Posts.Add(post); await _db.SaveChangesAsync(); }
public async Task AddAsync(string userId, int postId, string commentBody) { var comment = new PostComment { UserId = userId, PostId = postId, CommentBody = commentBody, DateAdded = DateTime.Now }; _context.PostComments.Add(comment); await _context.SaveChangesAsync(); }
public async Task <int> Create(PostAddModel model) { var post = new Post { Body = model.Body, UserId = model.UserId, TimeCreated = DateTime.Now }; var result = _db.Posts.Add(post); await _db.SaveChangesAsync(); return(result.Entity.Id); }
public virtual async Task<ActionResult> Edit(EditUserViewModel model) { if (ModelState.IsValid) { string userid = null; string username = model.OldUserName ?? model.UserName; using (var Db = new PortalDbContext()) { var user = Db.Users.First(u => u.UserName == username); // Update the user data: user.UserName = model.UserName; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; Db.Entry(user).State = System.Data.Entity.EntityState.Modified; userid = user.Id; await Db.SaveChangesAsync(); } // Update Facebook and Google id var usr = await UserManager.FindByIdAsync(userid); if (usr == null) { return View("Error"); } var userLogins = await UserManager.GetLoginsAsync(userid); // Facebook var fbLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Facebook"); if (fbLogin != null) { if (model.FacebookId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, fbLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (fbLogin.ProviderKey != model.FacebookId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.FacebookId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } // Google var gLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Google"); if (gLogin != null) { if (model.GoogleId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, gLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (gLogin.ProviderKey != model.GoogleId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.GoogleId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Google", model.GoogleId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } return RedirectToAction("Index"); } // If we got this far, something failed, redisplay form return View(model); }