public void Execute(CreateTeacherDto request) { if (_context.Teachers.Any(t => t.TFirstName == request.TFirstName && t.TLastName == request.TLastName)) { throw new EntityNotFoundException(); } if (!_context.Standards.Any(s => s.Id == request.StandardId)) { throw new EntityNotFoundException("Standard"); // Message -> Standard doesn't exist } _context.Teachers.Add(new Teacher { //Id = request.Id, TFirstName = request.TFirstName, TLastName = request.TLastName, Description = request.Description, Nationality = request.Nationality, CreatedAt = DateTime.Now, StandardId = request.StandardId }); _context.SaveChanges(); }
public void Execute(CreateTeacherDto request) { var editT = _context.Teachers.Find(request.Id); if (editT == null) { throw new EntityNotFoundException(); } if (editT.TFirstName != request.TFirstName) { if (_context.Teachers.Any(t => t.TFirstName == request.TFirstName && t.TLastName == request.TLastName)) { throw new EntityNotFoundException(); } editT.TFirstName = request.TFirstName; editT.TLastName = request.TLastName; editT.Description = request.Description; editT.Nationality = request.Nationality; editT.StandardId = request.StandardId; _context.SaveChanges(); } }
public ActionResult <IEnumerable <CreateTeacherDto> > Post([FromBody] CreateTeacherDto dto) { try { _addCommandTeacher.Execute(dto); return(StatusCode(StatusCodes.Status201Created)); } catch (EntityNotFoundException e) { return(UnprocessableEntity(e.Message)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public ActionResult <IEnumerable <CreateTeacherDto> > Put(int id, [FromBody] CreateTeacherDto dto) { dto.Id = id; try { _editCommandTeacher.Execute(dto); return(NoContent()); } catch (EntityNotFoundException e) { if (e.Message == "Teacher doesn't exist.") { return(NotFound(e.Message)); } return(UnprocessableEntity(e.Message)); } catch (Exception) { return(StatusCode(500, "error")); } }
public IActionResult Put([FromRoute] int classId, [FromRoute] int teacherId, [FromBody] CreateTeacherDto teacherDto) { _repository.UpdateTeacher(classId, teacherId, teacherDto); return(Created($"api/classes/{classId}/teachers/{teacherId}", null)); }
public async Task <IActionResult> Post([FromRoute] int classId, [FromBody] CreateTeacherDto teacherDto) { var teacherId = await _repository.CreateTeacher(classId, teacherDto); return(Created($"api/classes/{classId}/teachers/{teacherId}", null)); }