public async Task <IActionResult> Create(RegisterWorkerViewModel model) { if (ModelState.IsValid) { int TableOrganizations = _context.TableOrganizations.Include(i => i.users).FirstOrDefault (i => User.Identity.Name == i.users.UserName).TableOrganizationsId; Worker worker = new Worker { Surname = model.Surname, Name = model.Name, DoubleName = model.DoubleName, DateOfBirth = model.DateOfBirth, }; _context.Worker.Add(worker); await _context.SaveChangesAsync(); EmployeeRegistrationLog employee = new EmployeeRegistrationLog { EmployeeRegistrationDate = DateTime.Now, WorkerId = worker.WorkerId, TableOrganizationsId = TableOrganizations, }; _context.employeeRegistrationLogs.Add(employee); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } else { return(View(model)); } }
public async Task <ActionResult <User> > RegisterWorker([FromBody] RegisterWorkerViewModel model) { User user = await _context.Users.FirstOrDefaultAsync(u => u.Login == model.Login); if (user != null) { return(BadRequest()); } user = new User { Login = model.Login, Password = model.Password, FirstName = model.FirstName, LastName = model.LastName, Phone = model.Phone, Role = await _context.Roles.FirstOrDefaultAsync(r => r.Name == "Workman") }; _context.Users.Add(user); _context.Workers.Add(new Worker { Specal = model.Specal, User = user, UserId = user.Id }); await _context.SaveChangesAsync(); return(Ok()); }