Esempio n. 1
0
        public virtual async Task<T> Create(T obj)
        {
            _context.Add(obj);
            //context ja cria e retorna a entidade com id, retornar ela no obj
            await _context.SaveChangesAsync();

            return obj;
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([Bind("Id,UserName,PassWord,DisplayName,BirthDay,Phone,Address")] User user)
        {
            if (ModelState.IsValid)
            {
                user.CreateDate = DateTime.Now;
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
        public async Task CreateUser_WhenIdAlreadyExist_NullExpected()
        {
            // Arrange
            var initUser = InitUser();
            await _context.Users.AddAsync(initUser);

            await _context.SaveChangesAsync();

            // Act
            var result = await _userService.CreateUser(initUser);

            // Assert
            Assert.Null(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Add new user
        /// </summary>
        /// <param name="model">New user model</param>
        /// <returns>User model which was added</returns>
        public async Task <User> CreateUser(User model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var existUser = await _context.Users.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (existUser != null)
            {
                return(null);                   //TODO error model
            }
            await _context.Users.AddAsync(model);

            await _context.SaveChangesAsync();

            var result = await _context.Users.FirstOrDefaultAsync(x => x.Id == model.Id);

            return(result);
        }