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 <ThesisDTO> UpdateThesis(ThesisDTO thesisDTO) { try { Thesis thesis = await _context.Theses .Include(t => t.Subject) .Include(t => t.Professor) .Include(t => t.Comments) .Where(t => t.Id == thesisDTO.Id) .FirstOrDefaultAsync(); if (thesis.Professor.Id != thesisDTO.Professor.Id) { _logger.LogInformation("Unathorized attempt to update thesis " + thesis.Id + " by user " + thesisDTO.Professor.Id); return(null); } if (thesis.DateTaken != null) { _logger.LogInformation("Caanot update already taken thesis"); return(null); } if (thesis.DateCreated == null) { thesis.DateCreated = DateTime.Now; } else { thesis.DateUpdated = DateTime.Now; } thesis.Discription = thesisDTO.Discription; thesis.ShortDescription = thesisDTO.ShortDescription; thesis.Title = thesisDTO.Title; if (thesis.Subject == null || thesis.Subject.Id != thesisDTO.Subject.Id) { Subject newSubject = thesisDTO.Subject.ToSubject(); _context.Attach(newSubject); thesis.Subject = newSubject; } await _context.SaveChangesAsync(); return(thesis); } catch (Exception ex) { _logger.LogInformation(ex.Message); return(null); } }
public HttpResponseMessage Update(HttpRequestMessage request, ThesisDTO item) { if (item == null) { throw new ArgumentNullException("item"); } var dbObject = Mapper.Map <Thesis>(item); dbObject.Employees = null; _publicationBaseRepository.Update(dbObject); _db.SaveChanges(); dbObject = (Thesis)_publicationBaseRepository.Get(x => x.Id == dbObject.Id, null, x => x.Employees).SingleOrDefault(); foreach (var employee in dbObject.Employees.ToList()) { dbObject.Employees.Remove(employee); } foreach (var employee in item.Employees) { var employeeToAdd = _employeeRepository.GetById(employee.Id); if (employeeToAdd == null) { return(request.CreateErrorResponse( HttpStatusCode.PreconditionFailed, string.Format("Not found employee with id:{0}", employee.Id))); } dbObject.Employees.Add(employeeToAdd); } _db.SaveChanges(); var jobs = new PublicationsJobs(dbObject); jobs.Start(); var mapped = Mapper.Map <ThesisDTO>(dbObject); return(request.CreateResponse(HttpStatusCode.NoContent, mapped)); }
public HttpResponseMessage Add(HttpRequestMessage request, ThesisDTO item) { if (item == null) { throw new ArgumentNullException("item"); } var dbObject = Mapper.Map <Thesis>(item); dbObject.Employees = new List <Employee>(); if (item.Employees != null) { foreach (var employee in item.Employees) { var employeeToAdd = _employeeRepository.GetById(employee.Id); if (employeeToAdd == null) { return(request.CreateErrorResponse( HttpStatusCode.PreconditionFailed, string.Format("Not found employee with id:{0}", employee.Id))); } dbObject.Employees.Add(employeeToAdd); } } _publicationBaseRepository.Insert(dbObject); _db.SaveChanges(); var jobs = new PublicationsJobs(dbObject); jobs.Start(); var mapped = Mapper.Map <ThesisDTO>(dbObject); return(request.CreateResponse(HttpStatusCode.Created, mapped)); }