public Rsvp Create(Rsvp rsvp) { // validation if (string.IsNullOrWhiteSpace(rsvp.Email)) { throw new AppException("Email is required"); } Rsvp existing = GetByEmailAndECardId(rsvp.Email, rsvp.Id_EcardDetail); if (existing == null) { // check if local is not null if (existing != null) // I'm using a extension method { // detach _context.Entry(existing).State = EntityState.Detached; } _context.Rsvp.Add(rsvp); _context.SaveChanges(); } else { Update(rsvp); } return(rsvp); }
public ECardDetail Create(ECardDetail eCardDetail) { // validation if (string.IsNullOrWhiteSpace(eCardDetail.Title)) { throw new AppException("Title is required"); } _context.ECardDetail.Add(eCardDetail); _context.SaveChanges(); return(eCardDetail); }
public T Create(T entity) { // validation if (entity == null) { throw new AppException("entity cannot be null."); } _context.Add(entity); _context.SaveChanges(); return(entity); }
public User Create(User user, string password) { // validation if (string.IsNullOrWhiteSpace(password)) { throw new AppException("Password is required"); } if (_context.Users.Any(x => x.Username == user.Username)) { throw new AppException("Username \"" + user.Username + "\" is already taken"); } byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _context.Users.Add(user); _context.SaveChanges(); return(user); }