public async Task<IHttpActionResult> PutResident(int id, Resident resident) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != resident.Id) { return BadRequest(); } using (var transaction = db.Database.BeginTransaction()) { db.Entry(resident).State = EntityState.Modified; db.SaveChanges(); var rr = db.RelocationRecords.Find(resident.RelocationRecordId); if (rr.RelocationType.Equals("居住")) { db.Entry(rr).Collection(c => c.Residents).Load(); var sign = false; foreach (Resident r in rr.Residents) { db.Entry(r).Reload(); if (r.Status == 0) { sign = true; } } if (sign == false) { rr.Status = 1; } else { rr.Status = 0; } } else { rr.Status = 1; } db.Entry(rr).State = EntityState.Modified; await db.SaveChangesAsync(); transaction.Commit(); } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PutResident(int id, Resident resident) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != resident.Id) { return BadRequest(); } var resident_in_db = await db.Residents.FindAsync(id); if (resident_in_db.Status == 0 && resident.Status == 1) { resident.Status = 1; } else if(!resident.IdentityCard.Equals(resident_in_db.IdentityCard)) { resident.Status = db.Residents.Count(re => re.IdentityCard.Equals(resident.IdentityCard) && !re.Id.Equals(resident.Id)) > 0 ? 0 : 1; } db.Entry(resident_in_db).State = EntityState.Detached; db.Entry(resident).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ResidentExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostResident(Resident resident) { if (!ModelState.IsValid) { return BadRequest(ModelState); } using (var transaction = db.Database.BeginTransaction()) { var rr = db.RelocationRecords.Find(resident.RelocationRecordId); /** * Check resident validity: * 1. If rr RelocationType is '非居住', do not need to check the validity, resident status and rr status should be 1 * 2. If there is resident (RelocationType is '居住') with same IdentityCard, then resident status is 0 * 3. Otherwise, resident status is 1 **/ if (rr.RelocationType.Equals("居住")) { if (db.Residents.Count(re => re.IdentityCard.ToUpper().Equals(resident.IdentityCard.ToUpper()) && re.RelocationRecord.RelocationType.Equals("居住")) > 0) { // Resident invalid, this consequently causes rr Status to 0. resident.Status = 0; } else { resident.Status = 1; } } else { resident.Status = 1; } db.Residents.Add(resident); if (resident.Status == 0 && rr.Status != 0) { rr.Status = 0; db.Entry(rr).State = EntityState.Modified; } await db.SaveChangesAsync(); transaction.Commit(); } return CreatedAtRoute("DefaultApi", new { id = resident.Id }, resident); }
public async Task<IHttpActionResult> PostResident(Resident resident) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (db.Residents.Count(re => re.IdentityCard.Equals(resident.IdentityCard)) > 0) // resident already exist { resident.Status = 0; } db.Residents.Add(resident); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = resident.Id }, resident); }