Nancy.Response UpdateNotification(string id)
        {
            NotificationModel notification = null;

            // capture actual string posted in case the bind fails (as it will if the JSON is bad)
            // need to do it now as the bind operation will remove the data
            String rawBody = this.GetRawBody();

            try {
                notification = this.Bind <NotificationModel>();

                NotificationMapper not_mpr = new NotificationMapper();
                notification.Id = id;

                NotificationModel res = not_mpr.GetById(id);

                if (res == null)
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A notification with Id = {0} does not exist", id)));
                }
                not_mpr.update(notification);

                Nancy.Response response = Response.AsJson(notification);
                response.StatusCode = HttpStatusCode.OK;

                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + notification.Id;
                response.Headers["Location"] = uri;

                return(response);
            } catch (Exception e) {
                String operation = String.Format("NotificationModule.UpdateBadge({0})", (notification == null) ? "No Model Data" : notification.Message);
                return(HandleException(e, operation));
            }
        }
        Nancy.Response AddNotification()
        {
            // capture actual string posted in case the bind fails (as it will if the JSON is bad)
            // need to do it now as the bind operation will remove the data
            String rawBody = this.GetRawBody();

            NotificationModel notification = this.Bind <NotificationModel>();

            // Reject request with an ID param
            if (notification.Id != null)
            {
                return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "POST", HttpStatusCode.Conflict, String.Format("Use PUT to update an existing notification with Id = {0}", notification.Id)));
            }

            // Save the item to the DB
            try {
                NotificationMapper not_mpr = new NotificationMapper();
                notification.CreateId();
                not_mpr.Add(notification);
                Nancy.Response response = Response.AsJson(notification);
                response.StatusCode = HttpStatusCode.Created;

                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + notification.Id;
                response.Headers["Location"] = uri;

                return(response);
            } catch (Exception e) {
                Console.WriteLine(rawBody);
                String operation = String.Format("NotificationModule.AddItem({0})", (notification == null) ? "No Model Data" : notification.Message);
                return(HandleException(e, operation));
            }
        }
示例#3
0
        Nancy.Response AddUser()
        {
            // capture actual string posted in case the bind fails (as it will if the JSON is bad)
            // need to do it now as the bind operation will remove the data
            String rawBody = this.GetRawBody();

            UserModel user = this.Bind <UserModel>();

            // Reject request with an ID param
            if (user.Id != null)
            {
                return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "POST", HttpStatusCode.Conflict, String.Format("Use PUT to update an existing user with Id = {0}", user.Id)));
            }

            // Save the item to the DB
            try {
                UserMapper usr_mpr = new UserMapper();
                user.CreateId();
                usr_mpr.Add(user);

                //Get new user data from DB
                UserModel      new_user = usr_mpr.GetById(user.Id);
                Nancy.Response response = Response.AsJson(new_user);
                response.StatusCode = HttpStatusCode.Created;

                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + user.Id;
                response.Headers["Location"] = uri;

                return(response);
            } catch (Exception e) {
                Console.WriteLine(rawBody);
                String operation = String.Format("UserModule.AddItem({0})", (user == null) ? "No Model Data" : user.Username);
                return(HandleException(e, operation));
            }
        }
        Nancy.Response HandleException(Exception e, String operation)
        {
            String errorContext = String.Format("{1}:{2}: {3} Exception caught in: {0}", operation, DateTime.UtcNow.ToShortDateString(), DateTime.UtcNow.ToShortTimeString(), e.GetType());

            // write info to the server log.
            Console.WriteLine("----------------------\n{0}\n{1}\n--------------------", errorContext, e.Message);
            if (e.InnerException != null)
            {
                Console.WriteLine("{0}\n--------------------", e.InnerException.Message);
            }

            // Return generic message to user
            return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.InternalServerError, "Operational difficulties"));
        }
        Nancy.Response DeleteNotification(string id)
        {
            try {
                NotificationMapper not_mpr      = new NotificationMapper();
                NotificationModel  notification = not_mpr.GetById(id);

                if (notification == null)
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A notification with Id = {0} does not exist", id)));
                }

                not_mpr.delete(notification);

                return(HttpStatusCode.OK);
            } catch (Exception e) {
                return(HandleException(e, String.Format("\nNotificationModule.Delete({0})", id)));
            }
        }
示例#6
0
        Nancy.Response DeleteItem(string id)
        {
            try {
                ItemMapper itm_mpr = new ItemMapper();
                ItemModel  item    = itm_mpr.GetById(id);

                if (item == null)
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("An Item with Id = {0} does not exist", id)));
                }

                itm_mpr.delete(item);

                return(HttpStatusCode.OK);
            } catch (Exception e) {
                return(HandleException(e, String.Format("\nItemModule.Delete({0})", id)));
            }
        }
示例#7
0
        Nancy.Response DeleteUser(string id)
        {
            try
            {
                UserMapper usr_mpr = new UserMapper();
                UserModel  user    = usr_mpr.GetById(id);

                if (user == null)
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("An User with Id = {0} does not exist", id)));
                }

                usr_mpr.delete(user);

                return(HttpStatusCode.OK);
            }
            catch (Exception e)
            {
                return(HandleException(e, String.Format("\nUserModule.Delete({0})", id)));
            }
        }
        private object GetById(string id)
        {
            try
            {
                // create a connection to the PetaPoco orm and try to fetch and object with the given Id
                NotificationMapper not_mpr      = new NotificationMapper();
                NotificationModel  notification = not_mpr.GetById(id);

                if (notification == null)     // a null return means no object found
                // return a reponse conforming to REST conventions: a 404 error
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A notification with Id = {0} does not exist", id)));
                }
                else
                {
                    // success. The Nancy server will automatically serialise this to JSON
                    return(Response.AsJson(notification));
                }
            } catch (Exception e) {
                return(HandleException(e, String.Format("NotificationModule.GetById({0})", id)));
            }
        }
示例#9
0
        Nancy.Response UpdateItem(string id)
        {
            UserModel user = null;

            // capture actual string posted in case the bind fails (as it will if the JSON is bad)
            // need to do it now as the bind operation will remove the data
            String rawBody = this.GetRawBody();

            try {
                user = this.Bind <UserModel>();

                UserMapper usr_mpr = new UserMapper();
                user.Id = id;

                UserModel res = usr_mpr.GetById(id);

                if (res == null)
                {
                    return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A User with Id = {0} does not exist", id)));
                }
                usr_mpr.update(user);

                //Get new user data from DB
                UserModel updated_user = usr_mpr.GetById(user.Id);

                Nancy.Response response = Response.AsJson(updated_user);
                response.StatusCode = HttpStatusCode.OK;

                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + user.Id;
                response.Headers["Location"] = uri;

                return(response);
            } catch (Exception e) {
                String operation = String.Format("UserModule.UpdateBadge({0})", (user == null) ? "No Model Data" : user.Username);
                return(HandleException(e, operation));
            }
        }