public async Task <ActionResult <TheoryExam> > PostTheoryExam([FromForm] TheoryExam theoryExam) { var identity = User.Identity as System.Security.Claims.ClaimsIdentity; IEnumerable <System.Security.Claims.Claim> claims = identity.Claims; var national_code = claims.Where(p => p.Type == "national_code").FirstOrDefault()?.Value; var staff = _context.staffs.Where(q => q.national_code == national_code).FirstOrDefault(); if (staff == null) { return(Unauthorized()); } theoryExam.staff_id = staff.id; _context.theory_exams.Add(theoryExam); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTheoryExam", new { id = theoryExam.id }, theoryExam)); }
public async Task <IActionResult> PutTheoryExam(int id, [FromForm] TheoryExam theoryExam) { var identity = User.Identity as System.Security.Claims.ClaimsIdentity; IEnumerable <System.Security.Claims.Claim> claims = identity.Claims; var national_code = claims.Where(p => p.Type == "national_code").FirstOrDefault()?.Value; var staff = _context.staffs.Where(q => q.national_code == national_code).FirstOrDefault(); if (staff == null) { return(Unauthorized()); } theoryExam.staff_id = staff.id; if (id != theoryExam.id) { return(BadRequest()); } theoryExam.updated_at = DateTime.Now; _context.Entry(theoryExam).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TheoryExamExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }