예제 #1
0
        private Response UpdateBadge(int id)
        {
            Badge badge = null;

            try
            {
                badge = this.Bind <Badge>();

                BadgeContext ctx = new BadgeContext();
                ctx.update(badge);

                // 201 - created
                Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer());
                response.StatusCode = HttpStatusCode.OK;
                // uri
                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString();
                response.Headers["Location"] = uri;

                return(response);
            }
            catch (Exception e)
            {
                String operation = String.Format("BadgesModule.UpdateBadge({0})", (badge == null) ? "No Model Data" : badge.Title);

                var HandleException = (Response)operation;
                HandleException.StatusCode = HttpStatusCode.InternalServerError;
                return(HandleException);
            }
        }
예제 #2
0
        public List <Badge> SearchByName(string parameter = "")
        {
            if (parameter == "")
            {
                return(BadgeContext.GetDatabase().Fetch <Badge>("select * from Badges"));
            }

            string sql = String.Format("select * from Badges where title like '%{0}%' or description like '%{0}%'", parameter);

            return(BadgeContext.GetDatabase().Fetch <Badge>(sql));
        }
예제 #3
0
        private Response AddBadge()
        {
            Badge badge = null;

            try
            {
                // bind the request body to the object via a Nancy module.
                badge = this.Bind <Badge>();

                // check exists. Return 409 if it does
                if (badge.Id > 0)
                {
                    string errorMessage = String.Format("Use PUT to update an existing Badge with Id = {0}", badge.Id);

                    var ErrorResponse = (Response)errorMessage;
                    ErrorResponse.StatusCode = HttpStatusCode.Conflict;

                    return(ErrorResponse);
                }

                BadgeContext ctx = new BadgeContext();
                ctx.Add(badge);

                // 201 - created
                Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer());
                response.StatusCode = HttpStatusCode.Created;
                // uri
                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString();
                response.Headers["Location"] = uri;

                return(response);
            }
            catch (Exception e)
            {
                String operation = String.Format("BadgesModule.AddBadge({0})", (badge == null) ? "No Model Data" : badge.Title);

                var HandleException = (Response)operation;
                HandleException.StatusCode = HttpStatusCode.InternalServerError;
                return(HandleException);
            }
        }
예제 #4
0
        private Response Search(string param)
        {
            try
            {
                BadgeContext ctx   = new BadgeContext();
                List <Badge> badge = ctx.SearchByName(param);

                Nancy.Response response = new Nancy.Responses.JsonResponse <List <Badge> >(badge, new DefaultJsonSerializer());
                response.StatusCode = HttpStatusCode.OK;
                // uri
                string uri = this.Request.Url.SiteBase + this.Request.Path + "/";
                response.Headers["Location"] = uri;

                return(response);
            }
            catch (Exception e)
            {
                String operation = String.Format("BadgesModule.SearchByName({0})", (param == "") ? "No Model Data" : param.ToString());

                var HandleException = (Response)operation;
                HandleException.StatusCode = HttpStatusCode.InternalServerError;
                return(HandleException);
            }
        }
예제 #5
0
        private Response GetAll()
        {
            try
            {
                BadgeContext ctx    = new BadgeContext();
                List <Badge> badges = ctx.GetAll();

                Nancy.Response response = new Nancy.Responses.JsonResponse <List <Badge> >(badges, new DefaultJsonSerializer());
                response.StatusCode = HttpStatusCode.OK;
                // uri
                string uri = this.Request.Url.SiteBase + this.Request.Path + "/";
                response.Headers["Location"] = uri;

                return(response);
            }
            catch (Exception e)
            {
                String operation = String.Format("BadgesModule.GetAll()");

                var HandleException = (Response)operation;
                HandleException.StatusCode = HttpStatusCode.InternalServerError;
                return(HandleException);
            }
        }
예제 #6
0
        private Response GetById(int id)
        {
            try
            {
                BadgeContext ctx   = new BadgeContext();
                Badge        badge = ctx.GetById(id);

                Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer());
                response.StatusCode = HttpStatusCode.OK;
                // uri
                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString();
                response.Headers["Location"] = uri;

                return(response);
            }
            catch (Exception e)
            {
                String operation = String.Format("BadgesModule.GetById({0})", (id == 0) ? "No Model Data" : id.ToString());

                var HandleException = (Response)operation;
                HandleException.StatusCode = HttpStatusCode.InternalServerError;
                return(HandleException);
            }
        }
예제 #7
0
 public List <Badge> GetAll()
 {
     return(BadgeContext.GetDatabase().Fetch <Badge>("select * from Badges"));
 }
예제 #8
0
 internal void delete(Badge badge)
 {
     BadgeContext.GetDatabase().Delete(badge);
 }
예제 #9
0
 internal void update(Badge badge)
 {
     BadgeContext.GetDatabase().Update(badge);
 }
예제 #10
0
 public void Add(Badge badge)
 {
     BadgeContext.GetDatabase().Insert(badge);
 }
예제 #11
0
        public Badge GetById(int id)
        {
            String sql = "select * from Badges where Id =" + id.ToString();

            return(BadgeContext.GetDatabase().FirstOrDefault <Badge>(sql));
        }