Beispiel #1
0
        public static ApiResult Refresh(string _synappsDeviceId, List <PredictionEntityModel> _list)
        {
            var apiResult = new ApiResult {
                StatusCode = StatusCode.Success
            };
            var dc = new PredictionExamplesDataContext(SqlConnectionString);

            dc.Connection.Open();
            using (dc.Transaction = dc.Connection.BeginTransaction())
            {
                try
                {
                    var forDelete =
                        from n in dc.PredictionEntities
                        where n.SynAppsDeviceId == _synappsDeviceId
                        select n;
                    dc.PredictionEntities.DeleteAllOnSubmit(forDelete);

                    var now      = DateTime.UtcNow;
                    var entities = new List <PredictionEntity>();
                    foreach (var entity in _list)
                    {
                        entities.Add(new PredictionEntity
                        {
                            SynAppsDeviceId = entity.SynAppsDeviceId,
                            LuisExampleId   = entity.LuisExampleId,
                            EntityName      = entity.EntityName,
                            EntityValue     = entity.EntityValue,
                            CreatedAt       = now,
                            UpdatedAt       = now
                        });
                    }
                    dc.PredictionEntities.InsertAllOnSubmit(entities);
                    dc.SubmitChanges();
                    dc.Transaction.Commit();
                }
                catch (Exception e)
                {
                    dc.Transaction.Rollback();
                    apiResult.StatusCode = StatusCode.Error;
                    apiResult.Message    = e.Message;
                }
            }
            dc.Connection.Close();

            return(apiResult);
        }
Beispiel #2
0
        public ApiResult Save()
        {
            var apiResult = new ApiResult()
            {
                StatusCode = StatusCode.Success
            };
            var now = DateTime.UtcNow;
            var dc  = new PredictionExamplesDataContext(SqlConnectionString);

            if (this.Id == 0)
            {
                var r = new PredictionEntity();
                r.SynAppsDeviceId = this.SynAppsDeviceId;
                r.LuisExampleId   = this.LuisExampleId;
                r.EntityName      = this.EntityName;
                r.EntityValue     = this.EntityValue;
                r.CreatedAt       = now;
                r.UpdatedAt       = now;

                try
                {
                    dc.PredictionEntities.InsertOnSubmit(r);
                    dc.SubmitChanges();

                    this.Id        = r.Id;
                    this.CreatedAt = r.CreatedAt;
                    this.UpdatedAt = r.UpdatedAt;
                }
                catch (Exception e)
                {
                    apiResult.StatusCode = StatusCode.Error;
                    apiResult.Message    = e.Message;
                }
            }
            else
            {
                var records =
                    from n in dc.PredictionEntities
                    where n.Id == this.Id
                    select n;

                foreach (var r in records)
                {
                    r.SynAppsDeviceId = this.SynAppsDeviceId;
                    r.LuisExampleId   = this.LuisExampleId;
                    r.EntityName      = this.EntityName;
                    r.EntityValue     = this.EntityValue;
                    r.UpdatedAt       = now;
                }

                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception e)
                {
                    apiResult.StatusCode = StatusCode.Error;
                    apiResult.Message    = e.Message;
                }
            }

            return(apiResult);
        }