Ejemplo n.º 1
0
        public async Task <ActionResult <Guid> > Post([FromBody] ReadingRequest reading, CancellationToken cancellationToken)
        {
            var entity = await _context.Readings.AddAsync(new Reading
            {
                Value     = reading.Value,
                Scale     = reading.Scale,
                ReadingId = Guid.NewGuid(),
                Taken     = reading.Taken ?? DateTime.UtcNow
            }, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Entity.ReadingId);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <int> > Post([FromBody] UserRequest request)
        {
            var user = await _context
                       .Users
                       .AsNoTracking()
                       .FirstOrDefaultAsync(u => u.Username.ToLower() == request.Username.ToLower());

            if (user != null)
            {
                return(new BadRequestResult());
            }

            var salt = SaltGenerator.MakeSalty();
            var hash = await HashService.HashPassword(request.Password, salt);

            var result = _context.Users.Add(new User
            {
                Username  = request.Username,
                Firstname = request.Firstname,
                Lastname  = request.Lastname,
                Password  = hash,
                Salt      = salt,
                Created   = DateTime.UtcNow,
            });

            await _context.SaveChangesAsync();

            var roles = await _context.Roles.Where(role => request.Roles.Contains(role.Name)).ToListAsync();

            var userRoles = _context.UserRoles.AddRangeAsync(roles.Select(role => new UserRole
            {
                UserId = result.Entity.UserId,
                RoleId = role.RoleId,
            }).ToArray());

            await _context.SaveChangesAsync();

            return(result.Entity.UserId);
        }