public async Task UpdateSession(TbmUserSession sIn)
 {
     if (!TbmUserSessionExists(sIn.SessionId))
     {
         _usercontext.TbmUserSessions.Add(sIn);
         await _usercontext.SaveChangesAsync();
     }
     else
     {
         _usercontext.Entry(sIn).State = EntityState.Modified;
         await _usercontext.SaveChangesAsync();
     }
 }
        private async Task <List <SessionMobile> > GenSessionsTodayForDisplay(List <SessionMobile> sessionMobiles, TbmUserSessionUser su, TbmUserSession s, AmazonS3Client client)
        {
            //var segment = await _segments.Find<Segment>(
            //  w => w.SessionId == s.SessionId &&
            //    w.StartDate == Utility.GetStryyyyMMddNow() &&
            //    w.EndDate == Utility.GetStryyyyMMddNow()
            //  ).FirstOrDefaultAsync();

            var segment = await _usercontext.TbmUserSegments.Where(
                w =>
                w.SessionId == s.SessionId &&
                w.StartDate == Utility.GetStryyyyMMddNow() &&
                w.EndDate == Utility.GetStryyyyMMddNow()
                ).FirstOrDefaultAsync();


            if (segment != null)
            {
                //var userRegistration = await _userregistrations.Find<UserRegistration>(
                //w => w.SessionId == su.SessionId
                //&& w.UserId == su.UserId
                //&& (
                //    w.CheckInDate == Utility.GetStryyyyMMddNow()
                // || w.CheckOutDate == Utility.GetStryyyyMMddNow()
                //   )
                //).FirstOrDefaultAsync();

                var userRegistration = await _usercontext.TbUserRegistrations.Where(
                    w => w.SessionId == su.SessionId &&
                    w.UserId == su.UserId &&
                    (
                        w.CheckInDate == Utility.GetStryyyyMMddNow() ||
                        w.CheckOutDate == Utility.GetStryyyyMMddNow()
                    )
                    ).FirstOrDefaultAsync();


                SessionMobile sessionMobile = new SessionMobile();
                sessionMobile.UserId    = su.UserId;
                sessionMobile.SessionId = su.SessionId;
                if (userRegistration != null
                    &&
                    (
                        userRegistration.CheckInDatetime.ToString("yyyyMMdd") == Utility.GetStryyyyMMddNow() ||
                        userRegistration.CheckOutDatetime.ToString("yyyyMMdd") == Utility.GetStryyyyMMddNow()
                    )
                    )
                {
                    sessionMobile.IsCheckIn        = userRegistration.IsCheckIn;
                    sessionMobile.IsCheckOut       = userRegistration.IsCheckOut;
                    sessionMobile.CheckInDateTime  = userRegistration.CheckInDatetime;
                    sessionMobile.CheckOutDateTime = userRegistration.CheckOutDatetime;
                    sessionMobile.CheckInBy        = userRegistration.CheckInBy;
                    sessionMobile.CheckOutBy       = userRegistration.CheckOutBy;
                }

                if (
                    DateTime.Now >= segment.StartDateTime.AddHours(-2) &&
                    int.Parse(Utility.GetStrHHmmNow()) >= int.Parse(segment.StartDateTime.AddHours(-2).ToString("HHmm"))
                    )
                {
                    sessionMobile.CanCheckInOut = '1';
                }
                else
                {
                    sessionMobile.CanCheckInOut = '0';
                }


                sessionMobile.CourseId     = s.CourseId;
                sessionMobile.CourseName   = s.CourseName;
                sessionMobile.CourseNameTh = s.CourseNameTh;
                sessionMobile.SessionName  = s.SessionName;

                sessionMobile.StartDateTime = s.StartDateTime;
                sessionMobile.EndDateTime   = s.EndDateTime;

                if (segment != null)
                {
                    sessionMobile.SegmentStartDateTime = segment.StartDateTime;
                    sessionMobile.SegmentEndDateTime   = segment.EndDateTime;
                }

                sessionMobile.CourseOwnerEmail     = s.CourseOwnerEmail;
                sessionMobile.CourseOwnerContactNo = s.CourseOwnerContactNo;


                if (segment != null)
                {
                    sessionMobile.Venue       = segment.Venue;
                    sessionMobile.SegmentName = segment.SegmentName;
                }
                else
                {
                    sessionMobile.Venue = s.Venue;
                }


                sessionMobile.Instructor                   = s.Instructor;
                sessionMobile.CourseCreditHoursInit        = s.CourseCreditHoursInit;
                sessionMobile.PassingCriteriaExceptionInit = s.PassingCriteriaExceptionInit;
                sessionMobile.CourseCreditHours            = s.CourseCreditHours;
                sessionMobile.PassingCriteriaException     = s.PassingCriteriaException;
                sessionMobile.IsCancel = s.IsCancel;

                sessionMobile.CoverPhotoName = s.CoverPhotoName;
                sessionMobile.CoverPhotoUrl  = client.GetPreSignedURL(new GetPreSignedUrlRequest {
                    BucketName = _bucket, Key = sessionMobile.CoverPhotoName, Expires = DateTime.Now.AddMinutes(5)
                });                                                                                                                                                                         //s.CoverPhotoUrl;

                sessionMobiles.Add(sessionMobile);
            }
            else
            {
                // รอถามคุณก้อย ว่า กรณี Script วันทำอย่างไร

                //SessionMobile sessionMobile = new SessionMobile();
                //sessionMobile.UserId = su.UserId;
                //sessionMobile.SessionId = su.SessionId;
                //sessionMobile.CanCheckInOut = '0';
                //sessionMobile.CourseId = s.CourseId;
                //sessionMobile.CourseName = s.CourseName;
                //sessionMobile.CourseNameTh = s.CourseNameTh;
                //sessionMobile.SessionName = s.SessionName;

                //sessionMobile.StartDateTime = s.StartDateTime;
                //sessionMobile.EndDateTime = s.EndDateTime;

                //sessionMobile.CourseOwnerEmail = s.CourseOwnerEmail;
                //sessionMobile.CourseOwnerContactNo = s.CourseOwnerContactNo;

                //sessionMobile.Venue = s.Venue;

                //sessionMobile.Instructor = s.Instructor;
                //sessionMobile.CourseCreditHoursInit = s.CourseCreditHoursInit;
                //sessionMobile.PassingCriteriaExceptionInit = s.PassingCriteriaExceptionInit;
                //sessionMobile.CourseCreditHours = s.CourseCreditHours;
                //sessionMobile.PassingCriteriaException = s.PassingCriteriaException;
                //sessionMobile.IsCancel = s.IsCancel;

                //sessionMobiles.Add(sessionMobile);
            }


            return(sessionMobiles);
        }
        private List <SessionMobile> GenSessionsSevendayForDisplay(List <SessionMobile> sessionMobiles, TbmUserSessionUser su, TbmUserSession s, AmazonS3Client client)
        {
            SessionMobile sessionMobile = new SessionMobile();

            sessionMobile.UserId    = su.UserId;
            sessionMobile.SessionId = su.SessionId;

            sessionMobile.CanCheckInOut = '0';

            sessionMobile.CourseId             = s.CourseId;
            sessionMobile.CourseName           = s.CourseName;
            sessionMobile.CourseNameTh         = s.CourseNameTh;
            sessionMobile.SessionName          = s.SessionName;
            sessionMobile.StartDateTime        = s.StartDateTime;
            sessionMobile.EndDateTime          = s.EndDateTime;
            sessionMobile.CourseOwnerEmail     = s.CourseOwnerEmail;
            sessionMobile.CourseOwnerContactNo = s.CourseOwnerContactNo;

            sessionMobile.Venue = s.Venue;

            sessionMobile.Instructor                   = s.Instructor;
            sessionMobile.CourseCreditHoursInit        = s.CourseCreditHoursInit;
            sessionMobile.PassingCriteriaExceptionInit = s.PassingCriteriaExceptionInit;
            sessionMobile.CourseCreditHours            = s.CourseCreditHours;
            sessionMobile.PassingCriteriaException     = s.PassingCriteriaException;
            sessionMobile.IsCancel = s.IsCancel;

            sessionMobile.CoverPhotoName = s.CoverPhotoName;
            sessionMobile.CoverPhotoUrl  = client.GetPreSignedURL(new GetPreSignedUrlRequest {
                BucketName = _bucket, Key = sessionMobile.CoverPhotoName, Expires = DateTime.Now.AddMinutes(5)
            });                                                                                                                                                                           //s.CoverPhotoUrl;

            sessionMobiles.Add(sessionMobile);

            return(sessionMobiles);
        }
        public async Task <ActionResult> UpdateSession(TbmUserSession sIn)
        {
            await _userIjoinsService.UpdateSession(sIn);

            return(Ok());
        }