Beispiel #1
0
        private void OnSentenceExplain2VM(SqlDataReader reader, LearnEnSentenceExpViewModel vmExp)
        {
            var idx = 0;

            vmExp.ExpID       = reader.GetInt32(idx++);
            vmExp.LanguageKey = reader.GetString(idx++);
            vmExp.Detail      = reader.GetString(idx++);
        }
Beispiel #2
0
        public async Task <IActionResult> Get(int id, [FromQuery] Int32 hid = 0)
        {
            if (hid <= 0)
            {
                return(BadRequest("No Home Inputted"));
            }

            var usrObj  = HIHAPIUtility.GetUserClaim(this);
            var usrName = usrObj.Value;

            if (String.IsNullOrEmpty(usrName))
            {
                return(BadRequest("User cannot recognize"));
            }

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

            try
            {
                queryString = this.getQueryString(false, null, null, id, hid);

                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;
                    }

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

                    // Header
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            OnSentenceHeader2VM(reader, vm);
                            break; // Should only one result!!!
                        }
                    }
                    else
                    {
                        errorCode = HttpStatusCode.NotFound;
                        throw new Exception();
                    }
                    await reader.NextResultAsync();

                    // Explains
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            // Explain
                            var vmExp = new LearnEnSentenceExpViewModel();
                            OnSentenceExplain2VM(reader, vmExp);
                            vm.Explains.Add(vmExp);
                        }
                    }
                    await reader.NextResultAsync();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            // Word ID
                            vm.RelatedWordIDs.Add(reader.GetInt32(0));
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
                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));
        }