public async Task <ThesisDTO> CreateThesisIfNeeded(string professorId) { try { ThesisDTO exists = await _context.Theses .Where(t => t.Professor.Id == professorId && t.Title == null) .Include(t => t.Professor) .FirstOrDefaultAsync(); if (exists != null) { return(exists); } FacultyProfessor prof = new FacultyProfessor { Id = professorId }; _context.Attach(prof); Thesis newThesis = new Thesis { Professor = prof, DateCreated = DateTime.Now }; _context.Theses.Add(newThesis); await _context.SaveChangesAsync(); return(newThesis); } catch (Exception ex) { _logger.LogError(ex.Message); return(null); } }
public async Task <bool> ApproveRequest(int requestId) { try { Request request = await _context.Requests .Include(r => r.Thesis) .ThenInclude(t => t.Professor) .Where(r => r.Id == requestId && r.Status == (int)RequestStatus.Pending) .FirstOrDefaultAsync(); if (request.Thesis.Professor.Id != RequesterId) { return(false); } request.Status = (int)RequestStatus.Accepted; _context.Requests.Update(request); IEnumerable <Request> toDeny = await _context.Requests .Where(r => r.ThesisId == request.ThesisId && r.Id != requestId && r.Status == (int)RequestStatus.Pending) .ToArrayAsync(); foreach (Request r in toDeny) { r.Status = (int)RequestStatus.TakenBySomeoneElse; } Thesis thesis = await _context.Theses.FindAsync(request.ThesisId); thesis.DateTaken = DateTime.Now; FacultyStudent assignedStudent = await _context.FacultyStudents.FindAsync(request.StudentId); assignedStudent.AsignedThesis = thesis; _context.FacultyStudents.Update(assignedStudent); _context.Theses.Update(thesis); await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { _logger.LogInformation("Failed denying request: " + ex.Message); return(false); } }
public async Task <bool> AddProfessorToSubject(string professorId, int subjectId) { try { ProfessorSubject professorSubject = new ProfessorSubject { ProfessorId = professorId, SubjectId = subjectId }; _context.Attach(new FacultyProfessor { Id = professorId }); _context.Attach(new Subject { Id = subjectId }); _context.ProfessorSubjects.Add(professorSubject); await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }