Notes
Notes - notes.io |
{
public class CreateTeacherHandler : IRequestHandler<CreateTeacherRequestCommand, CreateTeacherResponseCommand>
{
private readonly IApplicationDbContext _appDbContext;
private readonly IMapper _mapper;
private readonly UserManager<SMSDomain.Identity.AppUser> _userManager;
private readonly RoleManager<AppRole> _roleManager;
public CreateTeacherHandler(IApplicationDbContext appDbContext, IMapper mapper, UserManager<SMSDomain.Identity.AppUser> userManager, RoleManager<AppRole> roleManager)
{
_appDbContext = appDbContext;
_mapper = mapper;
_userManager = userManager;
_roleManager = roleManager;
}
public string GenerateUniqueUsername(CreateTeacherRequestCommand request)
{
var username = request.Name.ToLower() + request.FathersName[0] + request.Surname[0] + request.Fin;
return username;
}
public string GenerateRandomPassword()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var random = new Random();
var password = new char[10];
for (int i = 0; i < password.Length - 1; i++)
{
password[i] = chars[random.Next(chars.Length)];
}
password[9] = '!';
return new string(password);
}
public async Task<CreateTeacherResponseCommand> Handle(CreateTeacherRequestCommand request, CancellationToken cancellationToken)
{
string username = GenerateUniqueUsername(request);
string password = GenerateRandomPassword();
if (await _userManager.Users.AnyAsync(teacher => teacher.Fin == request.Fin))
{
throw new Exception("A user with the same FIN already exists.");
}
else
{
//var studentCity = _appDbContext.Cities.Include(x => x.Id).FirstOrDefaultAsync(c => c.Name == request.Cityname);
SMSDomain.Entities.Teacher newTeacher = new SMSDomain.Entities.Teacher()
{
Name = request.Name,
Surname = request.Surname,
FathersName = request.FathersName,
Email = request.Email,
Age = request.Age,
Gender = request.Gender,
Fin = request.Fin,
Description = request.Description,
ActiveStatus = (bool)request.ActiveStatus,
Experience = request.Experience,
UserName = username,
BirthDate = request.BirthDate
};
//await _appDbContext.Students.AddAsync(newStudent);
//await _appDbContext.SaveChangesAsync(cancellationToken);
var a = await _userManager.CreateAsync(newTeacher, password);
await _userManager.AddToRoleAsync(newTeacher, "Teacher");
FirstLogin firstLogin = new()
{
UserId = newTeacher.Id,
};
await _appDbContext.FirstLogins.AddAsync(firstLogin);
//await _userManager.AddPasswordAsync(newStudent, password);
Address teacherAddress = new Address()
{
TeacherId = newTeacher.Id,
District = request.District,
StreetAddress = request.StreetAddress,
ZipCode = request.ZipCode,
HomeNumber = request.HomeNumber,
HouseNo = request.HouseNo,
};
await _appDbContext.Addresses.AddAsync(teacherAddress);
await _appDbContext.SaveChangesAsync(cancellationToken);
var phoneNumbers = new List<PhoneNumber>();
foreach (var number in request.PhoneNumbers)
{
phoneNumbers.Add(new PhoneNumber { Number = number, TeacherId = newTeacher.Id });
}
await _appDbContext.PhoneNumbers.AddRangeAsync(phoneNumbers);
await _appDbContext.SaveChangesAsync(cancellationToken);
// await _appDbContext.NumberPrefixes.AddAsync(studentNumberPrefix);
await _appDbContext.SaveChangesAsync(cancellationToken);
List<Course> courses = await _appDbContext.Courses.Where(c => c.Name == request.Course).ToListAsync();
foreach (var course in courses)
{
CourseTeacher teacherCourse = new CourseTeacher()
{
TeacherId = newTeacher.Id,
CourseId = course.Id
};
await _appDbContext.CourseTeacher.AddAsync(teacherCourse);
await _appDbContext.SaveChangesAsync(cancellationToken);
}
List<Group> groups = await _appDbContext.Groups.Where(c => c.Name == request.Group).ToListAsync();
foreach (var group in groups)
{
GroupTeacher teacherGroup = new GroupTeacher()
{
TeacherId = newTeacher.Id,
GroupId = group.Id
};
await _appDbContext.GroupTeacher.AddAsync(teacherGroup);
}
await _appDbContext.SaveChangesAsync(cancellationToken);
return new CreateTeacherResponseCommand()
{
Username = username,
Password = password,
Id = newTeacher.Id
};
}
}
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team