Example #1
0
 public async Task<IHttpActionResult> PostAppartment(Appartment appartment)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     using (var transaction = db.Database.BeginTransaction())
     {
         if (db.Appartments.Count(e => e.CommunityId == appartment.CommunityId
             && e.BuildingNumber == appartment.BuildingNumber && e.UnitNumber == e.UnitNumber
             && e.DoorNumber == appartment.DoorNumber) > 0)
         {
             return StatusCode(HttpStatusCode.Conflict);
         }
         if (appartment.Status == null)
         {
             appartment.Status = "可售";
         }
         db.Appartments.Add(appartment);
         await db.SaveChangesAsync();
         db.Changes.Add(Helper.Logger.NewRecord<Appartment>(appartment,RequestContext.Principal.Identity.Name));
         await db.SaveChangesAsync();
         db.Entry(appartment).Reference(b => b.Community).Load();
         transaction.Commit();
     }
     return CreatedAtRoute("DefaultApi", new { id = appartment.Id }, appartment);
 }
Example #2
0
        public async Task<IHttpActionResult> PutAppartment(int id, Appartment appartment)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != appartment.Id)
            {
                return BadRequest();
            }
            var origin = db.Appartments.Where(i => i.Id.Equals(id)).AsNoTracking().First();
            db.Changes.AddRange(Helper.Logger.ChangeRecords<Appartment>(origin, appartment, RequestContext.Principal.Identity.Name));
            db.Entry(appartment).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AppartmentExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }