Ejemplo n.º 1
0
        public JsonResult AssignLocation()
        {
            string json = Request.InputStream.ReadToEnd();
            AssingnLocationModel inputData =
                JsonConvert.DeserializeObject <AssingnLocationModel>(json);

            bool   success = false;
            string error   = "";
            PointSQLSrv1Entities entities = new PointSQLSrv1Entities();

            try
            {
                //haetaan ensin paikan id-numero koodin perusteella
                int locationId = (from l in entities.AssetLocation
                                  where l.Code == inputData.LocationCode
                                  select l.Id).FirstOrDefault();

                //haetaan laitteen id-numero koodin perusteella
                int assetId = (from a in entities.Assets
                               where a.Code == inputData.AssetCode
                               select a.Id).FirstOrDefault();

                if ((locationId > 0) && (assetId > 0))
                {
                    //tallennetaan uusi rivi aíkaleiman kanssa kantaan
                    AssetLocations newEntry = new AssetLocations();
                    newEntry.LocationId = locationId;
                    newEntry.AssetId    = assetId;
                    newEntry.LastSeen   = DateTime.Now;

                    entities.AssetLocations.Add(newEntry);
                    entities.SaveChanges();

                    success = true;
                }
            }
            catch (Exception ex)
            {
                error = ex.GetType().Name + ": " + ex.Message;
            }

            finally
            {
                entities.Dispose();
            }

            //palautetaan JSON-muotoinen tulos kutsujalle
            var result = new { success = success, error = error };

            return(Json(result));
        }