public async System.Threading.Tasks.Task <T> Add(T entity) { _ctx.Set <T>().Add(entity); await _ctx.SaveChangesAsync(); return(entity); }
public async Task <IActionResult> PutValue(int id, Value value) { if (id != value.ID) { return(BadRequest()); } _context.Entry(value).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ValueExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult> UpdateProfile(ProfileViewModel profileModel) { int currentUser = personRepository.GetIdByUserIdentityEmail((string)User.Identity.Name); Person personToUpdate = personRepository.GetPersonById(currentUser); personToUpdate.FirstName = profileModel.FirstName; personToUpdate.LastName = profileModel.LastName; personToUpdate.Description = profileModel.Description; personToUpdate.AccountHidden = profileModel.AccountHidden; if (ModelState.IsValid) { try { _context.Update(personToUpdate); await _context.SaveChangesAsync(); } catch (Exception) { TempData["ProcessMessage"] = "Could not update profile"; return(PartialView("Exception")); } } return(RedirectToAction("MyProfile")); }
/// <summary> /// Repository method to create new user in db /// </summary> /// <param name="newUser">Repository User Entity</param> /// <returns>User Entity</returns> public async Task <User> CreateNewUser(User newUser) { using (var context = new DatingAppContext()) { //validate that the new user entity isn't empty if (newUser == null) { throw new Exception("User not provided"); } //update status to be true and created / updates dates newUser.Status = true; newUser.CreatedDate = DateTime.Now; newUser.UpdatedDate = DateTime.Now; context.Users.Add(newUser); await context.SaveChangesAsync(); //pull user that has just been created var createdUser = await context.Users.FirstOrDefaultAsync(u => u.Username == newUser.Username); return(createdUser); } }
/// <summary> /// Repository method to delete existing user account /// </summary> /// <param name="userId">User Id</param> /// <returns>Completed Task</returns> public async Task DeleteUserAccount(long userId) { using (var context = new DatingAppContext()) { var existingUser = await context.Users.FirstOrDefaultAsync(u => u.Id == userId); context.Users.Remove(existingUser); await context.SaveChangesAsync(); } }
public async Task <User> Register(User user, string password) { byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); // user.PasswordHash = passwordHash; // user.PasswordSalt = passwordSalt; await context.Users.AddAsync(user); await context.SaveChangesAsync(); return(user); }
/// <summary> /// Repository method to update a user's login / logout status /// </summary> /// <param name="userName">Username</param> /// <returns>Completed Task</returns> public async Task UpdateUserStatus(long userId, bool status) { using (var context = new DatingAppContext()) { //pull user account var user = await context.Users.FirstOrDefaultAsync(u => u.Id == userId); //update status user.Status = status; await context.SaveChangesAsync(); } }
public async Task <IActionResult> Edit(int id, MovieDetails movieDetails, List <IFormFile> Image) { if (id != movieDetails.Id) { return(NotFound()); } if (ModelState.IsValid) { try { foreach (var item in Image) { if (item.Length > 0) { using var stream = new MemoryStream(); await item.CopyToAsync(stream); movieDetails.Image = stream.ToArray(); } } _context.Update(movieDetails); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MovieDetailsExists(movieDetails.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(movieDetails)); }
/// <summary> /// Repository method to update individual user's profile /// </summary> /// <param name="updatedUser">Repository User Entity</param> /// <returns>Completed Task</returns> public async Task UpdateUserProfile(User updatedUser) { using (var context = new DatingAppContext()) { var userProfile = await context.Users.FirstOrDefaultAsync(u => u.Username == updatedUser.Username); userProfile.FirstName = updatedUser.FirstName; userProfile.LastName = updatedUser.LastName; userProfile.Password = updatedUser.Password; userProfile.Location = updatedUser.Location; userProfile.Gender = updatedUser.Gender; userProfile.About = updatedUser.About; userProfile.Interests = updatedUser.Interests; userProfile.UpdatedDate = DateTime.Now; await context.SaveChangesAsync(); } }
public async Task <User> Register(UserRegisterDTO userDTO) { try { User user = _IMapper.Map <UserRegisterDTO, User>(userDTO); //HASH PASSWORD byte[] passwordHash, passwordSalt; Helper.CreatePasswordHash(userDTO.Password, out passwordHash, out passwordSalt); user.PasswordSalt = passwordSalt; user.PasswordHash = passwordHash; user.KnownAs = user.FullName; user.CreatedOn = DateTime.Now; //SAVE await _Context.Users.AddAsync(user); await _Context.SaveChangesAsync(); return(user); } catch (Exception ex) { return(null); } }
public async void UpdateAsync(TEntity entity) { _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(); }
public async Task <bool> SaveAll() { return(await context.SaveChangesAsync() > 0); }
public async Task <bool> SaveChanges() { try { return(await _context.SaveChangesAsync() > 0); } catch (Exception ex) { return(false); } }
public async Task <bool> SaveAll() { return(await _context.SaveChangesAsync() > 0 ? true : false); }