示例#1
0
        public async Task <User> UpdateUserAsync(UpsertUserParams ps)
        {
            _cache.Remove($"user-{ps.Username}");

            var oldUser = await _context.Users.FirstOrDefaultAsync(u => u.Username == ps.Username);

            if (oldUser == null)
            {
                throw new NotFoundException($"The user {ps.Username} cannot be found.");
            }

            // verification
            if (!oldUser.Email.RoughEquals(ps.Email) && await _context.Users.AsNoTracking().AnyAsync(u => u.Email == ps.Email))
            {
                throw new ConflictException($"User with email {ps.Email} is already existing.");
            }

            _mapper.Map(ps, oldUser);

            await _context.SaveChangesAsync();

            return(oldUser);
        }
示例#2
0
        public async Task <User> AddUserAsync(UpsertUserParams ps)
        {
            if (await _context.Users.AsNoTracking().AnyAsync(u => u.Username == ps.Username))
            {
                throw new ConflictException($"User with username {ps.Username} is already existing.");
            }

            // verification
            if (await _context.Users.AsNoTracking().AnyAsync(u => u.Email == ps.Email))
            {
                throw new ConflictException($"User with email {ps.Email} is already existing.");
            }

            var user = _mapper.Map <User>(ps);

            user.Password ??= "1"; // todo: use item in __scalpay

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }