Ejemplo n.º 1
0
        public async Task <IActionResult> RegisterAsync(UserRegisterDto model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Все поля обязательны к заполнению!"));
            }

            var user = await _farmAppContext.Users.FirstOrDefaultAsync(f => f.Login == model.Login);

            if (user != null)
            {
                return(BadRequest("Пользователь с таким Login занят!"));
            }

            CreatePasswordHash(model.Password, out var passwordHash, out var passwordSalt);

            var newUser = new User
            {
                Login        = model.Login,
                UserName     = model.FirstName + " " + model.LastName,
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt
            };

            await _farmAppContext.Users.AddAsync(newUser, cancellationToken);

            await _farmAppContext.SaveChangesAsync(cancellationToken);

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Role> > PostRoleAsync(Role role, CancellationToken cancellationToken = default)
        {
            if (role == null)
            {
                return(BadRequest("Тут пусто"));
            }
            /// Здесь костыль
            _farmAppContext.Roles.Add(role);
            await _farmAppContext.SaveChangesAsync(cancellationToken);

            return(Ok(role));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutVendorAsync([FromForm] int key, [FromForm] string values, CancellationToken cancellationToken = default)
        {
            if (key <= 0)
            {
                return(BadRequest("Key must be > 0"));
            }
            if (string.IsNullOrEmpty(values))
            {
                return(BadRequest("Value cannot be null or empty"));
            }

            var vendor = await _farmAppContext.Vendors.FirstOrDefaultAsync(f => f.Id == key, cancellationToken);

            if (vendor == null)
            {
                return(BadRequest($"Cannot be found Ath with key {key}"));
            }

            JsonConvert.PopulateObject(values, vendor);
            await _farmAppContext.SaveChangesAsync(cancellationToken);

            return(Ok());
        }