コード例 #1
0
        public IHttpActionResult PutCloth(string rfid, Cloth cloth)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (rfid != cloth.rfid)
            {
                return BadRequest();
            }

            db.Entry(cloth).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClothExists(rfid))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
コード例 #2
0
        public IHttpActionResult PostCloth(Cloth cloth)
        {
            if (!ClothExists(cloth.rfid))
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                cloth.createdAt = DateTime.Now;
                cloth.lastUpdatedAt = DateTime.Now;
                db.Clothes.Add(cloth);
                db.SaveChanges();
                //update to service bus to notify mobile app
                SendNotificationAsync(cloth.rfid);
            }
            else
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                Cloth updcloth = db.Clothes.Where(o=>o.rfid.Equals(cloth.rfid)).First();
                updcloth.image = cloth.image;
                updcloth.name = string.IsNullOrEmpty(cloth.name) ? updcloth.name:cloth.name ;
                updcloth.status = string.IsNullOrEmpty(cloth.status)? updcloth.status: cloth.status;
                updcloth.type = string.IsNullOrEmpty(cloth.type)?updcloth.type:cloth.type;
                updcloth.color = (!string.IsNullOrEmpty(cloth.color))? cloth.color: updcloth.color;
                updcloth.frequency = (cloth.frequency!=0 && cloth.frequency!=updcloth.frequency)?cloth.frequency: updcloth.frequency;
                updcloth.lastUpdatedAt = DateTime.Now;
                db.Entry(updcloth).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClothExists(cloth.rfid))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }

            }
            return CreatedAtRoute("DefaultApi", new { id = cloth.id }, cloth);
        }