public async Task <IActionResult> Post([FromBody] TeacherDTO dto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //TODO: AutoMapper Teacher teacher = new Teacher { Number = dto.Number, Name = dto.Name, Email = dto.Email, Password = dto.Password, IsAdmin = dto.IsAdmin }; if (!await _repo.AddAsync(teacher)) { throw new Exception("Unable to add teacher"); } return(CreatedAtRoute( Routes.TeacherEntry, new { number = teacher.Number }, _teachersRep.Entity(teacher) )); }
public async Task CreateAsync(Guid id, Guid userId) { var user = await _userRepository.GetOrFailAsync(userId); var teacher = await _teacherRepository.GetAsync(userId); if (teacher != null) { throw new ServiceException(ServiceErrorCodes.TeacherAlreadyExists, $"Teacher with id '{userId}' already exists"); } await _teacherRepository.AddAsync(new Teacher(id, user)); }
public async Task <ActionResult> Create(IFormCollection collection, Teacher t) { try { await teacherRepository.AddAsync(t); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <TeacherResponse> SaveAsync(Teacher teacher) { try { await _teacherRepository.AddAsync(teacher); await _unitOfWork.CompleteAsync(); // if everything is ok , then we can save the changes (transaction). return(new TeacherResponse(teacher)); } catch (Exception ex) { // Do some logging stuff return(new TeacherResponse($"An error occurred when saving the teacher: {ex.Message}")); } }
public async Task <IActionResult> Register(Teacher teacher, IFormFile image) { try { if (image != null) { using (var stream = new MemoryStream()) { image.CopyTo(stream); teacher.TeacherImage = stream.ToArray(); } } if (ModelState.IsValid) { await teacherRepository.AddAsync(teacher); await teacherRepository.SaveChangesAsync(); string fullName = teacher.FirstName + " " + teacher.SurName; await CreateTeacherAsUserAsync(teacher.TeacherID, teacher.Password, fullName); ViewBag.SuccessMsg = "Teacher Successfully Registered"; ModelState.Clear(); return(RedirectToAction("Details", new { id = teacher.TeacherID })); } return(View(teacher)); } catch (DataAccessException e) { ViewBag.ErrorMsg = ErrorProcessing.ProcessException("Data Access exception. ", e); return(RedirectToAction("Unknown", "Error")); } catch (Exception e) { ViewBag.ErrorMsg = ErrorProcessing.ProcessException("General exception. ", e); return(RedirectToAction("Unknown", "Error")); } }