Exemplo n.º 1
0
        public async Task <IHttpActionResult> PutSpots(string id, SpotsAuth spotsAuth)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Get Spot object from api object
            Spots spots = new Spots(spotsAuth.SpotID, spotsAuth.SpotName, spotsAuth.SensorID, spotsAuth.ImageType, spotsAuth.XPos, spotsAuth.YPos);

            if (id != spots.SpotID)
            {
                return(BadRequest());
            }

            // Check for correct AuthToken
            if (CheckAuthToken(spotsAuth.AuthToken) == null)
            {
                return(BadRequest("Incorrect Authtoken"));
            }

            // Check if spot alreadt exist and use it's data for missing fields
            Spots oldSpot = await db.Spots.FindAsync(id);

            if (oldSpot != null)
            {
                if (spots.SpotName == null)
                {
                    spots.SpotName = oldSpot.SpotName;
                }
                if (spots.SensorID == null)
                {
                    spots.SensorID = oldSpot.SensorID;
                }
                if (spots.ImageType == null)
                {
                    spots.ImageType = oldSpot.ImageType;
                }
                if (spots.XPos == null)
                {
                    spots.XPos = oldSpot.XPos;
                }
                if (spots.YPos == null)
                {
                    spots.YPos = oldSpot.YPos;
                }
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> PostSpots(SpotsAuth spotsAuth)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Get Spot object from api object
            Spots spots = new Spots(spotsAuth.SpotID, spotsAuth.SpotName, spotsAuth.SensorID, spotsAuth.ImageType, spotsAuth.XPos, spotsAuth.YPos);

            // Check for correct AuthToken
            if (CheckAuthToken(spotsAuth.AuthToken) == null)
            {
                return(BadRequest("Incorrect Authtoken"));
            }

            // Check any existing record of SpotID
            Spots oldSpot = await db.Spots.FindAsync(spots.SpotID);

            if (oldSpot == null)    // This is a new spot. Add it.
            {
                db.Spots.Add(spots);

                try {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateException) {
                    if (SpotsExists(spots.SpotID))
                    {
                        return(Conflict());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(CreatedAtRoute("DefaultApi", new { id = spots.SpotID }, spots));
            }
            else    // This spot alreay exists. Update with non null fields
            {
                if (spots.SpotName != null)
                {
                    oldSpot.SpotName = spots.SpotName;
                }
                if (spots.SensorID != null)
                {
                    oldSpot.SensorID = spots.SensorID;
                }
                if (spots.ImageType != null)
                {
                    oldSpot.ImageType = spots.ImageType;
                }
                if (spots.XPos != null)
                {
                    oldSpot.XPos = spots.XPos;
                }
                if (spots.YPos != null)
                {
                    oldSpot.YPos = spots.YPos;
                }

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

                try {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    if (!SpotsExists(spots.SpotID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(CreatedAtRoute("DefaultApi", new { id = oldSpot.SpotID }, oldSpot));
            }
        }