public void ProcessRequest(HttpContext context)
        {
            JsonBadge jsonResponse = null;
            try
            {
                if (context.Request.QueryString["BadgeId"] == null)
                {
                    throw new Exception("No badge id provided.");
                }

                int badgeId = 0;
                if (!int.TryParse(context.Request["BadgeId"].ToString(), out badgeId))
                {
                    throw new Exception("Invalid badge id provided.");
                }

                int patronId = 0;
                var patron = context.Session[SessionKey.Patron] as DAL.Patron;
                if (patron != null)
                {
                    patronId = patron.PID;
                }


                var badgeDetails = new Logic.Badge().GetForDisplay(context.Server, 
                    badgeId,
                    patronId);

                if (badgeDetails.Hidden == true && badgeDetails.Earned == false)
                {
                    throw new Exception("Secret badge must be earned to be revealed.");
                }

                jsonResponse = new JsonBadge()
                {
                    UserName = badgeDetails.DisplayName,
                    ImageUrl = VirtualPathUtility.ToAbsolute(badgeDetails.ImageUrl),
                    Description = context.Server.HtmlDecode(badgeDetails.Description),
                    HideEarn = badgeDetails.HideDefaultDescription,
                    Earn = badgeDetails.HowToEarn,
                    DateEarned = badgeDetails.DateEarned,
                    Hidden = badgeDetails.Hidden,
                    Success = true
                };
            }
            catch (Exception ex)
            {
                string safeBadgeId = context.Request["BadgeId"] == null
                    ? "<none requested>"
                    : context.Request["BadgeId"].ToString();
                this.Log().Error("Requested badge {0} from {1}?{2} - {3}",
                    safeBadgeId,
                    context.Request.Url,
                    context.Request.QueryString,
                    ex.Message);
                jsonResponse = new JsonBadge()
                {
                    Success = false,
                    ErrorMessage = ex.Message
                };
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(jsonResponse));
        }
        public void ProcessRequest(HttpContext context)
        {
            JsonBadge jsonResponse = null;

            try
            {
                if (context.Request.QueryString["BadgeId"] == null)
                {
                    throw new Exception("No badge id provided.");
                }

                int badgeId = 0;
                if (!int.TryParse(context.Request["BadgeId"].ToString(), out badgeId))
                {
                    throw new Exception("Invalid badge id provided.");
                }

                int patronId = 0;
                var patron   = context.Session[SessionKey.Patron] as DAL.Patron;
                if (patron != null)
                {
                    patronId = patron.PID;
                }


                var badgeDetails = new Logic.Badge().GetForDisplay(context.Server,
                                                                   badgeId,
                                                                   patronId);

                if (badgeDetails.Hidden == true && badgeDetails.Earned == false)
                {
                    throw new Exception("Secret badge must be earned to be revealed.");
                }

                jsonResponse = new JsonBadge()
                {
                    UserName    = badgeDetails.DisplayName,
                    ImageUrl    = VirtualPathUtility.ToAbsolute(badgeDetails.ImageUrl),
                    Description = context.Server.HtmlDecode(badgeDetails.Description),
                    HideEarn    = badgeDetails.HideDefaultDescription,
                    Earn        = badgeDetails.HowToEarn,
                    DateEarned  = badgeDetails.DateEarned,
                    Hidden      = badgeDetails.Hidden,
                    Success     = true
                };
            }
            catch (Exception ex)
            {
                string safeBadgeId = context.Request["BadgeId"] == null
                    ? "<none requested>"
                    : context.Request["BadgeId"].ToString();
                this.Log().Error("Requested badge {0} from {1}?{2} - {3}",
                                 safeBadgeId,
                                 context.Request.Url,
                                 context.Request.QueryString,
                                 ex.Message);
                jsonResponse = new JsonBadge()
                {
                    Success      = false,
                    ErrorMessage = ex.Message
                };
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(jsonResponse));
        }
Exemple #3
0
        public void ProcessRequest(HttpContext context)
        {
            var jsonResponse = new JsonBadge();

            if (context.Request.QueryString["BadgeId"] == null)
            {
                jsonResponse.Success      = false;
                jsonResponse.ErrorMessage = "No badge id provided.";
                this.Log().Error(string.Format("Badge requested from {0}?{1} with no id.",
                                               context.Request.Url,
                                               context.Request.QueryString));
            }
            else
            {
                int badgeId = 0;
                if (!int.TryParse(context.Request["BadgeId"].ToString(), out badgeId))
                {
                    jsonResponse.Success      = false;
                    jsonResponse.ErrorMessage = "Invalid badge id provided.";
                    this.Log().Error(string.Format("Requested badge {0} from {1}?{2} - invalid id",
                                                   context.Request["BadgeId"].ToString(),
                                                   context.Request.Url,
                                                   context.Request.QueryString));
                }
                else
                {
                    var badge = DAL.Badge.FetchObject(badgeId);
                    if (badge == null)
                    {
                        jsonResponse.Success      = false;
                        jsonResponse.ErrorMessage = "Badge not found.";
                        this.Log().Error(string.Format("Requested badge {0} from {1}?{2} - not found",
                                                       badgeId,
                                                       context.Request.Url,
                                                       context.Request.QueryString));
                    }
                    else
                    {
                        jsonResponse.UserName = badge.UserName;

                        string badgePath          = NoBadgePath;
                        string potentialBadgePath = string.Format("~/Images/Badges/{0}.png",
                                                                  badgeId);
                        if (System.IO.File.Exists(context.Server.MapPath(potentialBadgePath)))
                        {
                            badgePath = potentialBadgePath;
                        }

                        jsonResponse.ImageUrl = VirtualPathUtility.ToAbsolute(badgePath);

                        List <string> earn = new List <string>();

                        string earnText = DAL.Badge.GetBadgeReading(badgeId);
                        if (earnText.Length > 0)
                        {
                            earn.Add(string.Format("Earn points by reading: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetEnrollmentPrograms(badgeId);
                        if (earnText.Length > 0)
                        {
                            earn.Add(string.Format("Enroll in a reading program: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetBadgeBookLists(badgeId);
                        if (earnText.Length > 0)
                        {
                            earn.Add(string.Format("Complete a Challenge: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetBadgeGames(badgeId);
                        if (earnText.Length > 0)
                        {
                            earn.Add(string.Format("Unlock and complete an Adventure: {0}.",
                                                   earnText));
                        }

                        earnText = DAL.Badge.GetBadgeEvents(badgeId);
                        if (earnText.Length > 0)
                        {
                            earn.Add(string.Format("Attend an Event: {0}.", earnText));
                        }

                        jsonResponse.Earn    = earn.ToArray();
                        jsonResponse.Success = true;
                    }
                }
            }
            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(jsonResponse));
        }
        public void ProcessRequest(HttpContext context)
        {
            var jsonResponse = new JsonBadge();
            if(context.Request.QueryString["BadgeId"] == null) {
                jsonResponse.Success = false;
                jsonResponse.ErrorMessage = "No badge id provided.";
                this.Log().Error(string.Format("Badge requested from {0}?{1} with no id.",
                                               context.Request.Url,
                                               context.Request.QueryString));
            } else {
                int badgeId = 0;
                if(!int.TryParse(context.Request["BadgeId"].ToString(), out badgeId)) {
                    jsonResponse.Success = false;
                    jsonResponse.ErrorMessage = "Invalid badge id provided.";
                    this.Log().Error(string.Format("Requested badge {0} from {1}?{2} - invalid id",
                                                   context.Request["BadgeId"].ToString(),
                                                   context.Request.Url,
                                                   context.Request.QueryString));
                } else {
                    var badge = DAL.Badge.FetchObject(badgeId);
                    if(badge == null) {
                        jsonResponse.Success = false;
                        jsonResponse.ErrorMessage = "Badge not found.";
                        this.Log().Error(string.Format("Requested badge {0} from {1}?{2} - not found",
                                                       badgeId,
                                                       context.Request.Url,
                                                       context.Request.QueryString));
                    } else {
                        jsonResponse.UserName = badge.UserName;

                        string badgePath = NoBadgePath;
                        string potentialBadgePath = string.Format("~/Images/Badges/{0}.png",
                                                                  badgeId);
                        if(System.IO.File.Exists(context.Server.MapPath(potentialBadgePath))) {
                            badgePath = potentialBadgePath;
                        }

                        jsonResponse.ImageUrl = VirtualPathUtility.ToAbsolute(badgePath);

                        List<string> earn = new List<string>();

                        string earnText = DAL.Badge.GetBadgeReading(badgeId);
                        if(earnText.Length > 0) {
                            earn.Add(string.Format("Earn points by reading: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetEnrollmentPrograms(badgeId);
                        if(earnText.Length > 0) {
                            earn.Add(string.Format("Enroll in a reading program: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetBadgeBookLists(badgeId);
                        if(earnText.Length > 0) {
                            earn.Add(string.Format("Complete a Challenge: {0}.", earnText));
                        }

                        earnText = DAL.Badge.GetBadgeGames(badgeId);
                        if(earnText.Length > 0) {
                            earn.Add(string.Format("Unlock and complete an Adventure: {0}.",
                                                   earnText));
                        }

                        earnText = DAL.Badge.GetBadgeEvents(badgeId);
                        if(earnText.Length > 0) {
                            earn.Add(string.Format("Attend an Event: {0}.", earnText));
                        }

                        jsonResponse.Earn = earn.ToArray();
                        jsonResponse.Success = true;
                    }
                }
            }
            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(jsonResponse));
        }