public async Task <Post> Create(PostCreateInDto createOptions) { using (var transaction = await _context.Database.BeginTransactionAsync()) { var now = DateTime.Now; var newPost = new Post { StatusId = createOptions.StatusId, CategoryId = createOptions.CategoryId, CreateTime = now, UpdateTime = now, UserId = createOptions.UserId, Title = createOptions.Title, Content = createOptions.Content, Description = createOptions.Description }; await _context.Posts.AddAsync(newPost); await _context.SaveChangesAsync(); await _context.PostTagAssociations.AddRangeAsync(createOptions.TagIds.Select(t => new PostTagAssociation { TagId = t, PostId = newPost.Id })); await _context.SaveChangesAsync(); transaction.Commit(); return(newPost); } }
public async Task <User> Create(UserCreateInDto createParams) { #region create_user_if_existed_check var existed = await _context.Users.AnyAsync(u => u.Name == createParams.Name); if (existed) { return(await Task.FromException <User>(new ExistedConflictException())); } #endregion #region create_user using (var transaction = await _context.Database.BeginTransactionAsync()) { var createTime = DateTime.Now; var newUser = new User { Name = createParams.Name, CreateTime = createTime, UpdateTime = createTime, Password = string.Empty // here has not generated Id, thus cannot generate password }; newUser.Description = GetDefaultDescription(newUser); newUser.Avatar = _avatarService.GetDefaultAvatar(newUser); newUser.Url = _urlService.GetDefaultUrl(newUser); await _context.AddAsync(newUser); await _context.SaveChangesAsync(); newUser.Password = _passwordHasher.HashPassword(newUser.Id.ToString(), createParams.Password); var newUserProfile = new UserProfile { Id = newUser.Id, Misc = _profileService.GetDefaultMisc(newUser), Setting = _profileService.GetDefaultSetting(newUser), }; newUser.Profile = newUserProfile; await _context.AddAsync(newUserProfile); await _context.SaveChangesAsync(); await _roleService.AddRolePlayerWithoutCheck(newUser.Id, _roleService.GetDefaultId()); transaction.Commit(); return(newUser); } #endregion }
public async Task <UserRoleAssociation> AddRolePlayerWithoutCheck(Guid userId, int roleId) { var newUserRoleAssociation = new UserRoleAssociation { RoleId = roleId, UserId = userId, }; await _context.UserRoleAssociations.AddAsync(newUserRoleAssociation); await _context.SaveChangesAsync(); return(newUserRoleAssociation); }
public async Task <PostCategory> Create(TagCreateInDto createOptions) { if (await _context.PostCategories.AnyAsync(t => t.Name == createOptions.Name)) { throw new ExistedConflictException(); } var newCategory = new PostCategory { Name = createOptions.Name, Description = createOptions.Description }; await _context.PostCategories.AddAsync(newCategory); await _context.SaveChangesAsync(); return(newCategory); }
public async Task <Application> Create(ApplicationCreateInDto createOptions) { if (await _context.Applications.AnyAsync(a => a.Name == createOptions.Name)) { throw new ExistedConflictException(); } using (var transaction = await _context.Database.BeginTransactionAsync()) { var now = DateTime.Now; var application = new Application { Name = createOptions.Name, Version = createOptions.Version, Avatar = createOptions.Avatar, Url = createOptions.Url, Description = createOptions.Description, CreateTime = now, UpdateTime = now, }; await _context.Applications.AddAsync(application); await _context.SaveChangesAsync(); var applicationProfile = new ApplicationProfile { Id = application.Id, Misc = _applicationProfileService.GetDefaultMisc(application), Setting = _applicationProfileService.GetDefaultSetting(application), }; await _context.ApplicationProfiles.AddAsync(applicationProfile); await _context.SaveChangesAsync(); transaction.Commit(); return(application); } }
public async Task <Comment> Create(CommentCreateInDto createOptions) { if (!await _context.Posts.AnyAsync(p => p.Id == createOptions.PostId)) { throw new NotExistedException(); } var now = DateTime.Now; var comment = new Comment() { ParentId = createOptions.ParentId, CategoryId = createOptions.CategoryId, PostId = createOptions.PostId, UserId = createOptions.UserId, StatusId = createOptions.StatusId, Content = createOptions.Content, CreateTime = now, UpdateTime = now, }; await _context.Comments.AddAsync(comment); await _context.SaveChangesAsync(); return(comment); }