public async Task<IHttpActionResult> PostSubject(SubjectModel model) { if (!ModelState.IsValid) return BadRequest(ModelState); var newSubject = new Subject() { Name = model.name, TeacherId = model.teacherId, Type = (SubjectType)model.type, }; _context.Subjects.Add(newSubject); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { // if (ex.IsCausedByUniqueConstraintViolation()) throw new ApplicationException("Subject with such Key is already exists."); // else // throw; } model.id = newSubject.Id; return CreatedAtRoute("DefaultApi", new { id = model.id }, model); }
public async Task<IHttpActionResult> PutSubject(int id, SubjectModel model) { if (id != model.id) return BadRequest("No subject with such id found"); var subject = _context.Subjects.Where(x => x.Id == id).FirstOrDefault(); subject.TeacherId = model.teacherId; subject.Name = model.name; subject.Type = (SubjectType)model.type; _context.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = model.id }, model); }