Пример #1
0
        public async Task<bool> Update(Driver item)
        {
            var driver = await IdExist(item.Id);
            driver.IdCard = item.IdCard;
            driver.FirstName = item.FirstName;
            driver.LastNmae = item.LastNmae;
            driver.BirthDate = item.BirthDate;
            driver.Gender = item.Gender;
            driver.DriverIdCard = item.DriverIdCard;
            driver.ZipCode = item.ZipCode;
            driver.CategoryId = await CateExist(item.CategoryId);

            if (item.ExpireCard != null) driver.ExpireCard = item.ExpireCard.Value;
            if (item.TitleName != null) driver.TitleName = item.TitleName;

            _db.Entry(driver).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exception)
            {
                throw new DbUpdateConcurrencyException(exception.Message);
            }

            return true;
        }
Пример #2
0
        public async Task<Driver> Add(DriverModel item)
        {
                //add to asp.net Identity
            var usrIden = new IdentityUser(item.UserName);
            var usrResult = await UserManager.CreateAsync(usrIden, item.UserName);//pass is same username **by default**
            if (!usrResult.Succeeded)
            {
                throw new DbUpdateException(usrResult.Errors.First());
            }

            var roleResult = await UserManager.AddToRoleAsync(usrIden.Id, "driver");
            if (!roleResult.Succeeded)
            {
                throw new DbUpdateException(roleResult.Errors.First());
            }

            var driver = new Driver
            {
                IdCard = item.IdCard,
                FirstName = item.FirstName,
                LastNmae = item.LastNmae,
                BirthDate = item.BirthDate,
                Gender = item.Gender,
                DriverIdCard = item.DriverIdCard,
                ZipCode = item.ZipCode,
                CategoryId = await CateExist(item.CategoryId),
                AspId = usrIden.Id
            };

            if (item.ExpireCard != null) driver.ExpireCard = item.ExpireCard.Value;
            if (item.TitleName != null) driver.TitleName = item.TitleName;

            driver = _db.Drivers.Add(driver);
            try
            {
                await _db.SaveChangesAsync();
                return driver;
            }
            catch (DbUpdateConcurrencyException exception)
            {
                throw new DbUpdateConcurrencyException(exception.Message);
            }
        }