Translating this server side script to .net

Translating this server side script to .net

dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0

Hi all, I had a sudden need to implement a draggable row order and this example is perfect. I'm having a bit of a time converting the server side script to .net though, does anyone know if it has been done before? Specifically the .on events, I usually put my listeners in jquery so I'm not sure how that translates as I'm not a php person.

Below is my garden-variety server-side controller method I've been using:

         public ActionResult Method()
        {
            var dbType = Globals.dbType;
            var dbConnection = Globals.dbConnection;

            using (var db = new Database(dbType, dbConnection))
            {
                var response = new Editor(db, "table_name", "id")
                    .Model<DataModel>()
                    .TryCatch(false)
                    .Process(Request)
                    .Data();
                return Json(response);
            }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Hi,

    The .NET download package includes that example. This is is for .NET Core (which it looks like you are using):

        public class SequenceController : Controller
        {
            [Route("api/sequence")]
            [HttpGet]
            [HttpPost]
            public ActionResult Staff()
            {
                var dbType = Environment.GetEnvironmentVariable("DBTYPE");
                var dbConnection = Environment.GetEnvironmentVariable("DBCONNECTION");
    
                using (var db = new Database(dbType, dbConnection))
                {
                    var editor = new Editor(db, "audiobooks")
                        .Model<SequenceModel>()
                        .Field(new Field("title").Validator(Validation.NotEmpty()))
                        .Field(new Field("author").Validator(Validation.NotEmpty()))
                        .Field(new Field("duration").Validator(Validation.Numeric()))
                        .Field(new Field("readingOrder").Validator(Validation.Numeric()));
    
                    editor.PreCreate += (sender, e) => e.Editor.Db()
                        .Query("update", "audiobooks")
                        .Set("readingOrder", "readingOrder+1", false)
                        .Where("readingOrder", e.Values["readingOrder"], ">=")
                        .Exec();
    
                    editor.PreRemove += (sender, e) =>
                    {
                        // On remove, the sequence needs to be updated to decrement all rows
                        // beyond the deleted row. Get the current reading order by id (don't
                        // use the submitted value in case of a multi-row delete).
                        var order = e.Editor.Db()
                            .Select("audiobooks", new[] {"readingOrder"}, query => query.Where("id", e.Id))
                            .Fetch();
    
                        e.Editor.Db()
                            .Query("update", "audiobooks")
                            .Set("readingOrder", "readingOrder-1", false)
                            .Where("readingOrder", order["readingOrder"], ">")
                            .Exec();
                    };
    
                    var response = editor
                        .Process(Request)
                        .Data();
    
                    return Json(response);
                }
            }
        }
    

    Allan

  • dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0

    Oh awesome thanks Allan I forgot about the example solution you can download!

  • dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0

    @allan - Allan is there an easy way to sort the json in the controller based on the new sorting? ie. if you do a get on the same api endpoint the datatable uses, I want to sort the data object in the same order that the new order is defined as.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I'm afraid not since the ordering (sort) is applied by DataTables on the client-side. So it doesn't really matter what order it is read from the database (and thus what order it is in the JSON). Therefore Editor doesn't present an API for that.

    If you need the data to be ordered in the JSON (although I'm not sure why that is required?) you'd have to use a VIEW in SQL and tell Editor to read from that, rather than the table.

    Allan

This discussion has been closed.