public async Task <LecturalDTO> AddLecturalAsync(LecturalDTO lecturalDTO) { MilitaryRank militaryRank = await _context.MilitaryRank.Where(c => c.name == lecturalDTO.MilitaryRank).FirstOrDefaultAsync(); Position position = await _context.Position.Where(c => c.name == lecturalDTO.Position).FirstOrDefaultAsync(); AcademicDegree academicDegree = await _context.AcademicDegree.Where(c => c.name == lecturalDTO.AcademicDegree).FirstOrDefaultAsync(); AcademicTitle academicTitle = await _context.AcademicTitle.Where(c => c.name == lecturalDTO.AcademicTitle).FirstOrDefaultAsync(); Lectural newLecture = new Lectural(lecturalDTO, militaryRank, position, academicDegree, academicTitle); try { _context.Lectural.Add(newLecture); await _context.SaveChangesAsync(); return(lecturalDTO); } catch (DbUpdateConcurrencyException) { if (!LecturalExists(lecturalDTO.id)) { return(null); } else { throw; } } }
public async Task <LecturalDTO> DeleteLectureAsync(Guid id) { var lecture = await _context.Lectural.Where(c => c.id == id) .Include(c => c.MilitaryRank) .Include(c => c.Position) .Include(c => c.AcademicDegree) .Include(c => c.AcademicTitle) .FirstOrDefaultAsync(); if (lecture == null || lecture.id != id) { return(null); } LecturalDTO DTO = new LecturalDTO(lecture); _context.Lectural.Remove(lecture); try { await _context.SaveChangesAsync(); return(DTO); } catch (DbUpdateConcurrencyException) { if (!LecturalExists(id)) { return(null); } else { throw; } } }
public async Task <ActionResult <LecturalDTO> > PostLectural([FromBody] LecturalDTO lectural) { if (ModelState.IsValid) { var lec = await _lecturalService.AddLecturalAsync(lectural); return(CreatedAtAction("GetLectural", new { id = lec.id }, lec)); } return(BadRequest()); }
public async Task <IActionResult> PutLectural(Guid id, LecturalDTO lecturalDTO) { if (id != lecturalDTO.id) { return(BadRequest()); } LecturalDTO lectural = await _lecturalService.UpdateLecturalAsync(id, lecturalDTO); if (lectural != null) { return(CreatedAtAction("GetLectural", new { id = lectural.id }, lectural)); } return(NotFound()); }
public async Task <LecturalDTO> GetLecturalByIdAsync(Guid id) { var lecturalDB = await _context.Lectural.Where(c => c.id == id) .Include(c => c.Position) .Include(c => c.MilitaryRank) .Include(c => c.AcademicTitle) .Include(c => c.AcademicDegree) .FirstOrDefaultAsync(); if (lecturalDB != null) { LecturalDTO lecturalDTO = new LecturalDTO(lecturalDB); return(lecturalDTO); } return(null); }
public async Task <LecturalDTO> UpdateLecturalAsync(Guid id, LecturalDTO lecturalDTO) { var lectural = await _context.Lectural.Where(c => c.id == id) .Include(c => c.Position) .Include(c => c.MilitaryRank) .Include(c => c.AcademicTitle) .Include(c => c.AcademicDegree) .FirstOrDefaultAsync(); if (lectural == null || lectural.id != id) { return(null); } lectural.id = lecturalDTO.id; lectural.firstName = lecturalDTO.firstName; lectural.lastName = lecturalDTO.lastName; lectural.middleName = lecturalDTO.middleName; lectural.birthDay = lecturalDTO.birthDay; lectural.pathPhotoSmall = lecturalDTO.pathPhotoSmall; lectural.pathPhotoBig = lecturalDTO.pathPhotoBig; lectural.serialAndNumderMilitaryDocs = lecturalDTO.serialAndNumderMilitaryDocs; lectural.serialAndNumderCivilyDocs = lecturalDTO.serialAndNumderCivilyDocs; lectural.dateOfStartService = lecturalDTO.dateOfStartService; lectural.isMarried = lecturalDTO.isMarried; lectural.countOfChildren = lecturalDTO.countOfChildren; lectural.info = lecturalDTO.info; if (lectural.AcademicDegree != null) { if (lectural.AcademicDegree.name != lecturalDTO.AcademicDegree) { AcademicDegree academicDegree = _context.AcademicDegree.Where(c => c.name == lecturalDTO.AcademicDegree).FirstOrDefault(); lectural.AcademicDegree = academicDegree; } } else { AcademicDegree academicDegree = _context.AcademicDegree.Where(c => c.name == lecturalDTO.AcademicDegree).FirstOrDefault(); lectural.AcademicDegree = academicDegree; } if (lectural.AcademicTitle != null) { if (lectural.AcademicTitle.name != lecturalDTO.AcademicTitle) { AcademicTitle academicTitle = _context.AcademicTitle.Where(c => c.name == lecturalDTO.AcademicTitle).FirstOrDefault(); lectural.AcademicTitle = academicTitle; } } else { AcademicTitle academicTitle = _context.AcademicTitle.Where(c => c.name == lecturalDTO.AcademicTitle).FirstOrDefault(); lectural.AcademicTitle = academicTitle; } if (lectural.Position != null) { if (lectural.Position.name != lecturalDTO.Position) { Position position = _context.Position.Where(c => c.name == lecturalDTO.Position).FirstOrDefault(); lectural.Position = position; } } else { Position position = _context.Position.Where(c => c.name == lecturalDTO.Position).FirstOrDefault(); lectural.Position = position; } if (lectural.MilitaryRank != null) { if (lectural.MilitaryRank.name != lecturalDTO.MilitaryRank) { MilitaryRank militaryRank = _context.MilitaryRank.Where(c => c.name == lecturalDTO.MilitaryRank).FirstOrDefault(); lectural.MilitaryRank = militaryRank; } } else { MilitaryRank militaryRank = _context.MilitaryRank.Where(c => c.name == lecturalDTO.MilitaryRank).FirstOrDefault(); lectural.MilitaryRank = militaryRank; } _context.Entry(lectural).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return(lecturalDTO); } catch (DbUpdateConcurrencyException) { if (!LecturalExists(id)) { return(null); } else { throw; } } }