public async Task <IHttpActionResult> PuttUserActivity(int id, tUserActivity tUserActivity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tUserActivity.ID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> GettUserActivity(int id)
        {
            tUserActivity tUserActivity = await db.tUserActivities.FindAsync(id);

            if (tUserActivity == null)
            {
                return(NotFound());
            }

            return(Ok(tUserActivity));
        }
        public async Task <IHttpActionResult> PosttUserActivity(tUserActivity tUserActivity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.tUserActivities.Add(tUserActivity);
            await db.SaveChangesAsync();

            //  return CreatedAtRoute("DefaultApi", new { id = tUserActivity.ID }, tUserActivity);
            return(Ok(tUserActivity));
        }
        public async Task <IHttpActionResult> DeletetUserActivity(int id)
        {
            tUserActivity tUserActivity = await db.tUserActivities.FindAsync(id);

            if (tUserActivity == null)
            {
                return(NotFound());
            }

            db.tUserActivities.Remove(tUserActivity);
            await db.SaveChangesAsync();

            return(Ok(tUserActivity));
        }
        public async Task <IHttpActionResult> SoftDeleteUserActivity(int id, int status)
        {
            tUserActivity tUserActivity = await db.tUserActivities.FindAsync(id);

            if (tUserActivity == null)
            {
                return(NotFound());
            }
            tUserActivity.SystemStatusID      = status;
            tUserActivity.LastUpdatedDateTime = DateTime.Now;
            await db.SaveChangesAsync();

            return(Ok(tUserActivity));
        }
Example #6
0
        public IHttpActionResult Post(Activities value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (value.source == null || value.humanId == null)
            {
                return(BadRequest());
            }

            using (var dbContextTransaction = db.Database.BeginTransaction())
            {
                try
                {
                    tActivity activityObj = db.tActivities.SingleOrDefault(x => x.Name == value.type);

                    if (activityObj == null)
                    {
                        activityObj      = new tActivity();
                        activityObj.Name = value.type;

                        db.tActivities.Add(activityObj);
                    }

                    tSourceService sourceServiceObj = db.tSourceServices
                                                      .SingleOrDefault(x => x.ServiceName == value.source && x.SourceID == 5);

                    if (sourceServiceObj == null)
                    {
                        sourceServiceObj             = new tSourceService();
                        sourceServiceObj.ServiceName = value.source;
                        sourceServiceObj.TypeID      = 2; //Wellness
                        sourceServiceObj.SourceID    = 5; //HumanAPI

                        db.tSourceServices.Add(sourceServiceObj);
                    }

                    tUserSourceService userSourceServiceObj = null;

                    //Get credentials
                    tCredential credentialObj =
                        db.tCredentials.SingleOrDefault(x => x.SourceID == 5 &&
                                                        x.SourceUserID == value.humanId &&
                                                        x.SystemStatusID == 1);
                    if (credentialObj == null)
                    {
                        throw new NoUserCredentialsException("Unable to find any matching HAPI user credentials");
                    }
                    else
                    {
                        userSourceServiceObj = db.tUserSourceServices.SingleOrDefault(
                            x => x.SourceServiceID == sourceServiceObj.ID &&
                            x.CredentialID == credentialObj.ID &&
                            x.SystemStatusID == 1);

                        if (userSourceServiceObj == null)
                        {
                            userSourceServiceObj = new tUserSourceService();
                            userSourceServiceObj.SourceServiceID     = sourceServiceObj.ID;
                            userSourceServiceObj.UserID              = credentialObj.UserID;
                            userSourceServiceObj.CredentialID        = credentialObj.ID;
                            userSourceServiceObj.ConnectedOnDateTime = DateTime.Now;
                            userSourceServiceObj.LastSyncDateTime    = DateTime.Now;
                            userSourceServiceObj.LatestDateTime      = value.updatedAt;
                            userSourceServiceObj.StatusID            = 3; //connected
                            userSourceServiceObj.SystemStatusID      = 1; //valid
                            userSourceServiceObj.tCredential         = credentialObj;

                            db.tUserSourceServices.Add(userSourceServiceObj);
                        }
                        else
                        {
                            //update LatestDateTime to the most recent datetime
                            if (userSourceServiceObj.LatestDateTime == null ||
                                userSourceServiceObj.LatestDateTime < value.updatedAt)
                            {
                                userSourceServiceObj.LatestDateTime = value.updatedAt;
                            }
                        }
                    }

                    tUserActivity userActivity = null;
                    userActivity = db.tUserActivities
                                   .SingleOrDefault(x => x.SourceObjectID == value.id);

                    if (userActivity == null)
                    {
                        userActivity                     = new tUserActivity();
                        userActivity.UserID              = credentialObj.UserID;
                        userActivity.SourceObjectID      = value.id;
                        userActivity.UserSourceServiceID = sourceServiceObj.ID;
                        userActivity.ActivityID          = activityObj.ID;

                        //Dates
                        DateTimeOffset dtoStart, dtoEnd;
                        if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.startTime,
                                                                                         value.tzOffset,
                                                                                         out dtoStart))
                        {
                            userActivity.StartDateTime = dtoStart;
                        }
                        else
                        {
                            userActivity.StartDateTime = value.startTime;
                        }

                        if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.endTime, value.tzOffset,
                                                                                         out dtoEnd))
                        {
                            userActivity.EndDateTime = dtoEnd;
                        }
                        else
                        {
                            userActivity.EndDateTime = value.endTime;
                        }

                        userActivity.Duration           = value.duration;
                        userActivity.DurationUOMID      = 8;
                        userActivity.Distance           = value.distance;
                        userActivity.DistanceUOMID      = 9;
                        userActivity.Steps              = value.steps;
                        userActivity.Calories           = value.calories;
                        userActivity.SystemStatusID     = 1;
                        userActivity.tUserSourceService = userSourceServiceObj;

                        db.tUserActivities.Add(userActivity);
                    }
                    else
                    {
                        userActivity.ActivityID = activityObj.ID;

                        //Dates
                        DateTimeOffset dtoStart, dtoEnd;
                        if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.startTime,
                                                                                         value.tzOffset,
                                                                                         out dtoStart))
                        {
                            userActivity.StartDateTime = dtoStart;
                        }
                        else
                        {
                            userActivity.StartDateTime = value.startTime;
                        }

                        if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.endTime, value.tzOffset,
                                                                                         out dtoEnd))
                        {
                            userActivity.EndDateTime = dtoEnd;
                        }
                        else
                        {
                            userActivity.EndDateTime = value.endTime;
                        }

                        userActivity.Duration            = value.duration;
                        userActivity.DurationUOMID       = 8;
                        userActivity.Distance            = value.distance;
                        userActivity.DistanceUOMID       = 9;
                        userActivity.Steps               = value.steps;
                        userActivity.Calories            = value.calories;
                        userActivity.SystemStatusID      = 1;
                        userActivity.tUserSourceService  = userSourceServiceObj;
                        userActivity.LastUpdatedDateTime = DateTime.Now;
                    }

                    db.SaveChanges();

                    dbContextTransaction.Commit();

                    return(Ok(userActivity));
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();

                    //Insert Error Log
                    tUserDataErrLog userErrorLog = new tUserDataErrLog();

                    userErrorLog.ErrTypeID   = (int)ErrorLogging.enumErrorType.Application;
                    userErrorLog.ErrSourceID = (int)AuditLogging.enumApplication.SFCBAL;
                    userErrorLog.Code        = ex.HResult.ToString();
                    userErrorLog.Description = ex.Message;
                    userErrorLog.Trace       = ex.StackTrace;

                    dbErr.tUserDataErrLogs.Add(userErrorLog);
                    dbErr.SaveChanges();

                    string ErrMsg = "An error occured and we have logged the error. Please try again later.";

                    Exception Err = new Exception(ErrMsg, ex);

                    return(InternalServerError(Err));
                }
            }
        }