Beispiel #1
0
        public async Task <EntityOperationResult <User> > Register(User user, string password)
        {
            var result = new EntityOperationResult <User>();

            if (string.IsNullOrWhiteSpace(password))
            {
                result.AddError("Password", "Password is required");
                return(result);
            }

            var userRepeated = await _context.Users.FirstOrDefaultAsync(x => x.Spa.Id == user.SpaId && x.Active && x.Name == user.Name);

            if (userRepeated != null)
            {
                result.AddError("Repeated user", "Username \"" + user.Name + "\" is already taken");
                return(result);
            }

            var spaAssociated = await _context.Spas.FirstOrDefaultAsync(x => x.Id == user.SpaId);

            if (spaAssociated == null)
            {
                result.AddError("Spa", "Spa \"" + user.SpaId + "\" is not already exist");
                return(result);
            }

            CreatePasswordHash(password, out var passwordHash, out var passwordSalt);

            var newUser = new User
            {
                Email        = user.Email,
                Active       = true,
                Name         = user.Name,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                SpaId        = user.SpaId
            };

            _context.Users.Add(newUser);
            await _context.SaveChangesAsync();

            return(new EntityOperationResult <User>(newUser));
        }
Beispiel #2
0
        public async Task <EntityOperationResult <Spa> > AddSpa(string name)
        {
            var spa = await _context.Spas.FirstOrDefaultAsync(x => x.Name == name);

            if (spa != null)
            {
                var result = new EntityOperationResult <Spa>();
                result.AddError("Duplicado", "Ya existe el nombre del balneario");
                return(result);
            }

            var newSpa = new Spa
            {
                Name   = name,
                Active = true
            };

            _context.Spas.Add(newSpa);

            await _context.SaveChangesAsync();

            return(new EntityOperationResult <Spa>(newSpa));
        }