public async Task <IActionResult> AddOfficer(OfficerDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } User user = await _userManager.FindByEmailAsync(model.UserName); if (user != null) { return(BadRequest(ModelState)); } var now = DateTime.Now; user = new User() { SecurityStamp = Guid.NewGuid().ToString(), UserName = model.UserName, CreatedDate = now, LastModifiedDate = now }; Random rnd = new Random(); int num = rnd.Next(1, 9); string password = PinGenerator.RandomString(4) + "@" + num; var result = await _userManager.CreateAsync(user, password); await _userManager.AddToRoleAsync(user, "Medical Officer"); if (result.Succeeded) { var medical = new MedicalOfficer() { UserName = model.UserName, Firstname = model.Firstname, Lastname = model.Lastname, FacilityId = model.FacilityId, Department = model.Department, ContactNo = model.ContactNo, CreatedBy = model.CreatedBy, Created = DateTime.Now }; _db.Officers.Add(medical); if (await _db.SaveChangesAsync() > 0) { // SendSms(model.UserName, password, "Donor Trace", model.ContactNo); return(Ok()); } } return(BadRequest("Could not save officer")); }
public async Task <Buyer> RegisterUser(string email) { using (var uow = _dalfactory.GetUnitOfWork()) { var user = uow.Repository <Buyer>() .FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase)); string pin; var client = new EmailService(); IdentityMessage message; if (user == null) { _logger.Info("User not found"); do { pin = PinGenerator.RandomString(4); }while (uow.Repository <Buyer>().Exist(u => u.Pin.Equals(pin))); user = new Buyer() { Balance = 0, Email = email, Pin = pin, IsDeleted = false }; message = new IdentityMessage() { Body = $"Hello. Your PIN is {pin}. Use this to shoping in {ConfigurationManager.AppSettings["CompanyName"]} SnackBar.", Subject = "New PIN", Destination = email }; user = uow.Repository <Buyer>().AddOrUpdate(user); uow.Save(); _logger.Info($"User Created with PIN = {user.Pin}"); } else { pin = user.Pin; message = new IdentityMessage() { Body = $"Hello. Your PIN is {pin}.", Subject = "Forgot PIN?", Destination = email }; _logger.Info("User Found"); } await client.SendAsync(message).ConfigureAwait(false); return(user); } }