예제 #1
0
        public async Task <IActionResult> Get([FromQuery] Int32 hid, Int32 top = 100, Int32 skip = 0)
        {
            if (hid <= 0)
            {
                return(BadRequest("HID is missing"));
            }

            String usrName = String.Empty;

            if (Startup.UnitTestMode)
            {
                usrName = UnitTestUtility.UnitTestUser;
            }
            else
            {
                var usrObj = HIHAPIUtility.GetUserClaim(this);
                usrName = usrObj.Value;
            }
            if (String.IsNullOrEmpty(usrName))
            {
                return(BadRequest("User cannot recognize"));
            }

            BaseListViewModel <EventHabitViewModel> listVm = new BaseListViewModel <EventHabitViewModel>();
            SqlConnection  conn        = null;
            SqlCommand     cmd         = null;
            SqlDataReader  reader      = null;
            String         queryString = "";
            String         strErrMsg   = "";
            HttpStatusCode errorCode   = HttpStatusCode.OK;

            try
            {
                using (conn = new SqlConnection(Startup.DBConnectionString))
                {
                    await conn.OpenAsync();

                    // Check Home assignment with current user
                    try
                    {
                        HIHAPIUtility.CheckHIDAssignment(conn, hid, usrName);
                    }
                    catch (Exception)
                    {
                        errorCode = HttpStatusCode.BadRequest;
                        throw;
                    }

                    queryString = HIHDBUtility.Event_GetEventHabitQueryString(true, usrName, hid, skip, top);

                    cmd    = new SqlCommand(queryString, conn);
                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            listVm.TotalCount = reader.GetInt32(0);
                            break;
                        }
                    }
                    reader.NextResult();

                    if (reader.HasRows)
                    {
                        Dictionary <Int32, EventHabitViewModel> dictVM = new Dictionary <int, EventHabitViewModel>();
                        while (reader.Read())
                        {
                            EventHabitViewModel vm     = new EventHabitViewModel();
                            EventHabitDetail    detail = new EventHabitDetail();
                            HIHDBUtility.Event_HabitDB2VM(reader, vm, detail, true);
                            if (dictVM.ContainsKey(vm.ID))
                            {
                                dictVM[vm.ID].Details.Add(detail);
                            }
                            else
                            {
                                vm.Details.Add(detail);
                                listVm.Add(vm);
                                dictVM.Add(vm.ID, vm);
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(exp.Message);
#endif
                strErrMsg = exp.Message;
                if (errorCode == HttpStatusCode.OK)
                {
                    errorCode = HttpStatusCode.InternalServerError;
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }

            if (errorCode != HttpStatusCode.OK)
            {
                switch (errorCode)
                {
                case HttpStatusCode.Unauthorized:
                    return(Unauthorized());

                case HttpStatusCode.NotFound:
                    return(NotFound());

                case HttpStatusCode.BadRequest:
                    return(BadRequest(strErrMsg));

                default:
                    return(StatusCode(500, strErrMsg));
                }
            }

            var setting = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateFormatString = HIHAPIConstants.DateFormatPattern,
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };

            return(new JsonResult(listVm, setting));
        }
예제 #2
0
        public async Task <IActionResult> Get(int id, [FromQuery] Int32 hid = 0)
        {
            if (hid <= 0)
            {
                return(BadRequest("HID is missing"));
            }

            String usrName = String.Empty;

            if (Startup.UnitTestMode)
            {
                usrName = UnitTestUtility.UnitTestUser;
            }
            else
            {
                var usrObj = HIHAPIUtility.GetUserClaim(this);
                usrName = usrObj.Value;
            }
            if (String.IsNullOrEmpty(usrName))
            {
                return(BadRequest("User cannot recognize"));
            }

            EventHabitViewModel vm     = new EventHabitViewModel();
            SqlConnection       conn   = null;
            SqlCommand          cmd    = null;
            SqlDataReader       reader = null;
            String         queryString = "";
            String         strErrMsg   = "";
            HttpStatusCode errorCode   = HttpStatusCode.OK;

            try
            {
                using (conn = new SqlConnection(Startup.DBConnectionString))
                {
                    await conn.OpenAsync();

                    // Check Home assignment with current user
                    try
                    {
                        HIHAPIUtility.CheckHIDAssignment(conn, hid, usrName);
                    }
                    catch (Exception)
                    {
                        errorCode = HttpStatusCode.BadRequest;
                        throw;
                    }

                    queryString = HIHDBUtility.Event_GetEventHabitQueryString(false, usrName, hid, null, null, id);

                    cmd    = new SqlCommand(queryString, conn);
                    reader = cmd.ExecuteReader();

                    // Detail
                    while (reader.Read())
                    {
                        EventHabitDetail detail = new EventHabitDetail();
                        HIHDBUtility.Event_HabitDB2VM(reader, vm, detail, false);
                        vm.Details.Add(detail);
                    }
                    reader.NextResult();

                    // Checkin
                    while (reader.Read())
                    {
                        EventHabitCheckInViewModel civm = new EventHabitCheckInViewModel();
                        civm.ID       = reader.GetInt32(0);
                        civm.TranDate = reader.GetDateTime(1);
                        if (!reader.IsDBNull(2))
                        {
                            civm.Score = reader.GetInt32(2);
                        }
                        if (!reader.IsDBNull(3))
                        {
                            civm.Comment = reader.GetString(3);
                        }
                        if (!reader.IsDBNull(4))
                        {
                            civm.CreatedBy = reader.GetString(4);
                        }
                        if (!reader.IsDBNull(5))
                        {
                            civm.CreatedAt = reader.GetDateTime(5);
                        }
                        if (!reader.IsDBNull(6))
                        {
                            civm.UpdatedBy = reader.GetString(6);
                        }
                        if (!reader.IsDBNull(7))
                        {
                            civm.UpdatedAt = reader.GetDateTime(7);
                        }
                        vm.CheckInLogs.Add(civm);
                    }
                }
            }
            catch (Exception exp)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(exp.Message);
#endif
                strErrMsg = exp.Message;
                if (errorCode == HttpStatusCode.OK)
                {
                    errorCode = HttpStatusCode.InternalServerError;
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }

            if (errorCode != HttpStatusCode.OK)
            {
                switch (errorCode)
                {
                case HttpStatusCode.Unauthorized:
                    return(Unauthorized());

                case HttpStatusCode.NotFound:
                    return(NotFound());

                case HttpStatusCode.BadRequest:
                    return(BadRequest(strErrMsg));

                default:
                    return(StatusCode(500, strErrMsg));
                }
            }

            var setting = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateFormatString = HIHAPIConstants.DateFormatPattern,
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };

            return(new JsonResult(vm, setting));
        }