public async Task <Response> AddCourse(Owner owner, Course course) { Response response = new Response(); try { _context.Entry(owner).State = EntityState.Detached; _context.Entry(course).State = EntityState.Detached; OwnerCourse ownerCourse = new OwnerCourse() { OwnerID = owner.ID, CourseID = course.ID }; (await _context.Owners.Include(c => c.Courses).Where(c => c.ID == owner.ID).FirstOrDefaultAsync()).Courses.Add(ownerCourse); await _context.SaveChangesAsync(); return(response); } catch (Exception e) { StringBuilder sb = new StringBuilder(); log.Error(sb.AppendLine(e.Message).AppendLine(e.StackTrace).ToString()); response.Success = false; response.ErrorList.Add("Error while addind course to owner."); return(response); } }
public async Task <IActionResult> PutStudent([FromRoute] int id, [FromBody] Student student) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != student.Id) { return(BadRequest()); } _context.Entry(student).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <DataResponse <int> > CreateAndReturnID(Class item) { DataResponse <int> response = new DataResponse <int>(); try { item.CreatedAt = DateTime.Now; await _context.Classes.AddAsync(item); await _context.SaveChangesAsync(); await _context.Entry(item).GetDatabaseValuesAsync(); response.Data.Add(item.ID); return(response); } catch (Exception e) { response.Success = false; if (e.Message.Contains("Name")) { response.ErrorList.Add("Name is required."); } if (e.Message.Contains("Period")) { response.ErrorList.Add("Period is required."); } else { response.ErrorList.Add("Error while adding Course."); } StringBuilder sb = new StringBuilder(); log.Error(sb.AppendLine(e.Message).AppendLine(e.StackTrace).ToString()); return(response); } }