public async Task <IActionResult> PutAccount([FromRoute] long id, [FromBody] AccountInfomation account) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != account.Id) { return(BadRequest()); } _context.Entry(account).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccountExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create([Bind("Id,Email,Password,Salt,RollNumber,CreatedAt,UpdatedAt,DeletedAt,Status")] Account account) { if (ModelState.IsValid) { _context.Add(account); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(account)); }
public async Task <IActionResult> Create([Bind("Id,Name,Description,Status")] Subject subject) { if (ModelState.IsValid) { _context.Add(subject); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(subject)); }
public async Task <IActionResult> PostLogin([FromBody] Login login) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existAccount = _context.Account.SingleOrDefault(a => a.Email == login.Email); if (existAccount != null) { if (existAccount.Password == PasswordHandle.GetInstance().EncryptPassword(login.Password, existAccount.Salt)) { var existCredential = await _context.Credential.SingleOrDefaultAsync(c => c.OwnerId == existAccount.Id); if (existCredential != null) { var accessToken = PasswordGenerator.Generate(length: 40, allowed: Sets.Alphanumerics); existCredential.AccessToken = accessToken; await _context.SaveChangesAsync(); return(Ok(existCredential)); } else { var credential = new Credential(existAccount.Id) { AccessToken = PasswordGenerator.Generate(length: 40, allowed: Sets.Alphanumerics) }; _context.Credential.Add(credential); await _context.SaveChangesAsync(); return(Ok(credential)); } } return(BadRequest("Mật khẩu không chính xác!")); } return(BadRequest("Email hoặc mật khẩu không chính xác!")); }
public async Task <IActionResult> Create([Bind("Id,Name,StartTime,EndTime,IntendTime,Status")] Class @class) { if (ModelState.IsValid) { _context.Add(@class); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(@class)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,BirthDay,Phone")] AccountInfomation accountInfomation, int[] classIds, int roleId) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var Rnb = ""; switch (roleId) { case 1: Rnb = "A"; break; case 2: Rnb = "M"; break; case 3: Rnb = "D"; break; default: return(BadRequest()); } //Generate RollNumber var count = await _context.Account.CountAsync(a => a.RollNumber.Contains(Rnb)) + 1; string rollNumber; if (count < 10) { rollNumber = "0000" + count; } else if (count < 100) { rollNumber = "000" + count; } else if (count < 1000) { rollNumber = "00" + count; } else if (count < 10000) { rollNumber = "0" + count; } else { rollNumber = count.ToString(); } var rnber = (Rnb + rollNumber).ToLower(); // Generate Email var str = accountInfomation.FirstName.Split(" "); string email = accountInfomation.LastName; foreach (var item in str) { if (item.Any()) { email += item[0]; } } email = email.ToLower(); var emailGenerate = RemoveUTF8.RemoveSign4VietnameseString(email + rnber + "@siingroup.com").ToLower(); var passwordGenerate = RemoveUTF8.RemoveSign4VietnameseString(email + rnber); //Create new account Account account = new Account { RollNumber = rnber, Email = emailGenerate, Salt = PasswordHandle.GetInstance().GenerateSalt() }; account.Password = PasswordHandle.GetInstance().EncryptPassword(passwordGenerate, account.Salt); _context.Account.Add(account); //Create thông tin đăng nhập để trả về response Login login = new Login { Email = emailGenerate, Password = passwordGenerate }; //Check uniqe by phone if (AccountExistsByPhone(accountInfomation.Phone)) { return(Conflict("Tài khoản đã tồn tại trên hệ thống, vui lòng kiểm tra lại!")); } else { //Save account await _context.SaveChangesAsync(); //Get ra account.Id để gán cho FK ownerId bên accountinfomation accountInfomation.OwnerId = account.Id; _context.AccountInfomation.Add(accountInfomation); await _context.SaveChangesAsync(); AccountLogsDefault log = new AccountLogsDefault { Title = "Đã tạo tài khoản với email " + login.Email + "!" }; _context.Default.Add(log); AccountLogs al = new AccountLogs { OwnerId = account.Id, CreatedBy = accountInfomation.FirstName + " " + accountInfomation.LastName, Default = log }; _context.Log.Add(al); await _context.SaveChangesAsync(); foreach (var item in classIds) { Classes classes = new Classes { OwnerId = account.Id, ClassId = item }; _context.Classes.Add(classes); AccountLogsDefault logs = new AccountLogsDefault(); var classAccount = _context.Class.SingleOrDefault(a => a.Id == classes.ClassId); logs.Title = accountInfomation.FirstName + " " + accountInfomation.LastName + " đã xếp bạn vào lớp " + classAccount.Name; _context.Default.Add(logs); AccountLogs als = new AccountLogs { OwnerId = account.Id, CreatedBy = accountInfomation.FirstName + " " + accountInfomation.LastName, Default = logs }; _context.Log.Add(als); } await _context.SaveChangesAsync(); } return(Created("", login)); }