Beispiel #1
0
        public async Task <IActionResult> Create([Bind("ApplicationId,Name")] Application application)
        {
            if (ModelState.IsValid)
            {
                application.ApplicationId = Guid.NewGuid();
                _context.Add(application);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(application));
        }
Beispiel #2
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Name")] FModel fModel)
        {
            if (ModelState.IsValid)
            {
                db.MyModel.Add(fModel);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(fModel));
        }
Beispiel #3
0
        public async Task <IActionResult> Create([Bind("ClientId,Username,Password")] Client client)
        {
            if (ModelState.IsValid)
            {
                client.ClientId = Guid.NewGuid();
                _context.Add(client);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(client));
        }
Beispiel #4
0
        public async Task <IActionResult> Create([Bind("ClientId,ApplicationId")] Scope scope)
        {
            if (ModelState.IsValid)
            {
                _context.Add(scope);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ApplicationId"] = new SelectList(_context.Applications, "ApplicationId", "ApplicationId", scope.ApplicationId);
            ViewData["ClientId"]      = new SelectList(_context.Clients, "ClientId", "ClientId", scope.ClientId);
            return(View(scope));
        }
        public async Task HandleAsync(string key, CustomerUpdatedEvent @event)
        {
            var user = await _dbContext.Users.FirstOrDefaultAsync(s => s.Email == @event.Email);

            if (user == null)
            {
                throw new ApplicationException("Email is not found.");
            }

            user.FirstName = @event.FirstName;
            user.LastName  = @event.LastName;

            await _dbContext.SaveChangesAsync();
        }
        public async Task <UserOwnership> AddUserOwnershipAsync(UserOwnership userOwnership)
        {
            using (var _context = new IdentityDBContext(_dbOptions))
            {
                var exists = GetUserOwnershipAsync(userOwnership).Result;
                if (exists != null)
                {
                    return(exists);
                }

                _context.UserOwnership.Add(userOwnership);
                await _context.SaveChangesAsync();

                return(GetUserOwnershipAsync(userOwnership).Result);
            }
        }
        public async Task <UserAgenda> DeleteUserAgenda(string userId, int conferenceId, int sessionId, int talkId)
        {
            using (var _context = new IdentityDBContext(_dbOptions))
            {
                var task = await _context.UserAgenda.FindAsync(userId, conferenceId, sessionId, talkId);

                if (task == null)
                {
                    return(null);
                }

                _context.UserAgenda.Remove(task);
                await _context.SaveChangesAsync();

                return(task);
            }
        }
        public async Task <UserOwnership> DeleteUserOwnership(string userId, int conferenceId)
        {
            using (var _context = new IdentityDBContext(_dbOptions))
            {
                var task = await _context.UserOwnership.FindAsync(userId, conferenceId);

                if (task == null)
                {
                    return(null);
                }

                _context.UserOwnership.Remove(task);
                await _context.SaveChangesAsync();

                return(task);
            }
        }
Beispiel #9
0
        //批量添加

        /*
         *如果您给我的答案如上,我还是认可的,要是第一种真的说不过去了啊。经过如上操作依然有问题,
         *我们将所有记录添加到同一上下文实例,这意味着EF会跟踪这十万条记录, 对于刚开始添加的几个记录,
         *会运行得很快,但是当越到后面数据快接近十万时,EF正在追踪更大的对象图,您觉得恐怖不,
         *这就是您不懂EF原理的代价,还对其进行诟病,吐槽性能可以,至少保证您写的代码没问题吧,
         *我们进一步优化需要关闭自调用的DetectChanges方法无需进行对每一个添加的实体进行扫描
         */
        public async Task <IActionResult> EmployeeAddRange()
        {
            var newEmpCount = 12;
            int indexI, indexII, indexIII = 0;
            var emps = new List <Employee>();

            for (var i = 0; i < newEmpCount; i++)
            {
                indexI   = rander.Next(0, Surname.Length);
                indexII  = rander.Next(0, LastName.Length);
                indexIII = rander.Next(0, LastName.Length);
                emps.Add(new Employee {
                    Name = $"{Surname[indexI]}{LastName[indexII]}{LastName[indexIII]}"
                });
            }
            _dbContext.Employees.AddRange(emps);
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("EmployeeView"));
        }
        public async Task <ActionResult <dynamic> > Register(Users user)
        {
            TryValidateModel(user);
            if (ModelState.IsValid)
            {
                user.Status = "Not Verified";
                await db.Users.AddAsync(user);

                await db.SaveChangesAsync();
                await SendVerificationMailAsync(user);

                return(Created("", new
                {
                    user.Id, user.FullName, user.UserName, user.Email
                }));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Beispiel #11
0
        public async Task <ActionResult <dynamic> > RegisterUser([FromBody] UserInfo user)
        {
            //TryValidateModel(user); //controller method used to validate the model forcefully. It not mandatory as validation is automatically done.
            if (ModelState.IsValid)
            {
                var result = await db.Users.AddAsync(user);

                await db.SaveChangesAsync();

                //return Created("", result.Entity);
                return(Created("", new {
                    Email = result.Entity.Email,
                    FirstName = result.Entity.FirstName,
                    LastName = result.Entity.LastName,
                    CreatedDate = DateTime.Now
                }));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Beispiel #12
0
        public async Task <ActionResult <dynamic> > RegisterUser([FromBody] UserInfo user)
        {
            TryValidateModel(user);
            if (ModelState.IsValid)
            {
                var result = await db.Users.AddAsync(user);

                await db.SaveChangesAsync();

                return(Created("", new
                {
                    Email = result.Entity.Email,
                    FirstName = result.Entity.FirstName,
                    LastName = result.Entity.LastName,
                    CreateDate = DateTime.Now
                }));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Beispiel #13
0
        public async Task <ServiceResponse <int> > Register(Users user, string password)
        {
            ServiceResponse <int> response = new ServiceResponse <int>();

            if (await UserExists(user.Username))
            {
                response.Message = "User already exists.";
                response.Success = false;
                return(response);
            }

            CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            response.Data = user.Id;
            return(response);
        }
Beispiel #14
0
        protected override async Task Handle(RegisterUserCommand command, CancellationToken cancellationToken)
        {
            if (await _dbContext.Users.AsNoTracking().AnyAsync(s => s.Email == command.Email))
            {
                throw new ApplicationException("Email is already exist.");
            }

            var user = new User
            {
                Id        = command.Id,
                Password  = command.Password,
                Email     = command.Email,
                FirstName = command.FirstName,
                LastName  = command.LastName,
                Address   = command.Address
            };

            _dbContext.Users.Add(user);

            await _dbContext.SaveChangesAsync();

            await _bus.PublishAsync(command.Email, user);
        }
Beispiel #15
0
        protected override async Task Handle(RegisterUserCommand command, CancellationToken cancellationToken)
        {
            if (await _dbContext.Users.AsNoTracking().AnyAsync(s => s.Email == command.Email))
            {
                throw new ApplicationException("Email is already exist.");
            }

            var user = new User
            {
                Id        = command.Id,
                Password  = command.Password,
                Email     = command.Email,
                FirstName = command.FirstName,
                LastName  = command.LastName,
            };

            var outboxEvent = new Outbox
            {
                Id            = Guid.NewGuid(),
                AggregateId   = command.Id,
                AggregateType = "User",
                Type          = "UserCreated",
                Payload       = JsonSerializer.Serialize(new UserCreatedEvent
                {
                    Id        = user.Id,
                    Email     = user.Email,
                    LastName  = user.LastName,
                    FirstName = user.FirstName,
                })
            };

            _dbContext.Users.Add(user);

            _dbContext.OutboxEvents.Add(outboxEvent);

            await _dbContext.SaveChangesAsync();
        }