Skip to content Skip to sidebar Skip to footer

Mvc 4 Controler - Using Dynamic Staffid's & Businessid's Instead Of Hard Coded Ones

I am building an online booking system so that people can book doctor appointments online. I have roughly built my MVC 4 controller so that I can test it with the stored procedure

Solution 1:

maybe you also want to use mvc routing for this, add a new route (having a ScheduleController)

//Added UrlParameter.Optional so you can set Default values
routes.MapRoute(
    name: "Schedule",
    url: "Schedule/Get/{start}/{id}",
    defaults: new { 
                   controller = "Schedule", 
                   action = "index", 
                   start = UrlParameter.Optional, 
                   id = UrlParameter.Optional 
              }
);

That way you can just call the action method with a url like

   http://mydomain.com/Bookings/Get/2013-2-23/1-3,2-5

If the controller-action index has the signature

public ActionResult Index(DateTime? start, string id = "")

MVCs model binding will then bind the params from the url automatically to the signatures variables.

Create a model in your models Folder "MyJsonModels.cs" and fill it with life...

publicclassMydate
{
    publicstring ndate { get; set; }
    public MyDateNDay[] dates { get; set; }
    publicstring pdate { get; set; }
}

publicclassMyDateNDay {
    publicstring date {get; set;}
    publicstring day { get; set; }
}

publicclassMystaff
{
    public Staff[] staff { get; set; }
}

publicclassStaff
{
    publicint StaffID { get; set; }
    publicint BusinessID { get; set; }
    public SlotDate[] SlotDates {get; set; }
}

publicclassSlotDate
{
    publicstring Date { get; set; }
    publicstring SlotDay { get; set; }
    public TimeSlot[] slots { get; set; }
}

publicclassTimeSlot
{
    publicint SlotID { get; set; }
    publicstring SlotTime { get; set; }
}

publicclassInputData
{
    publicint SlotID { get; set; }
    publicint StaffID { get; set; }
    publicint BusinessID { get; set; }
    public DateTime SlotDate { get; set; }
    publicstring SlotDay { get; set; }
    publicstring SlotTime { get; set; }

    publicInputData(int sid, int stid, int bid, DateTime day, string time )
    {
        SlotID = sid;
        StaffID = stid;
        BusinessID = bid;
        SlotDate = day;
        SlotDay = day.DayOfWeek.ToString();
        SlotTime = time;
    }
}

Then inside the Index method you would have to implement the real action... (this will not create the exact result you are asking for, i merely show how you can use linq in order to simplify your code and to remove redundancy. Maybe you would use self-created objects to store the results instead of using KeyValuePairs. Again, its just a quick, but working example)

public ActionResult Index(DateTime? start, string id = "")
    {

        varstartdate= start ?? DateTime.Today;

        // split the input into anonymous objects containing staffid and businessidvarstaffids= from staffid in id.Split(',').Select(x => x.Split('-'))
                       select new { sid = int.Parse(staffid[0]), bid = int.Parse(staffid[1]) };

        // get the days you needvardays= Enumerable.Range(0, 7).Select(x => startdate.AddDays(x));

        // create myDatesinti=0;
        MydateMyDates=newMydate() {
            ndate = days.First().AddDays(7).ToString("yyyy-MM-dd"),
            pdate = days.First().AddDays(-7).ToString("yyyy-MM-dd"),
            dates = (
                        from day in days
                            select newMyDateNDay { 
                               date = day.ToShortDateString(), 
                               day  = day.DayOfWeek.ToString()
                            }
                    ).ToArray()
        };

        // example InputData - Array as it might be returned by the sql procedure/* I added this Array to simulate a possible procedure result your DB could return,
           maybe you can even add a new procedure that would return the data just as in the
           following Array
        */varinput=new [] { 
           newInputData( 40501, 1, 1, newDateTime(2013, 02, 20), "09:00"),
           newInputData( 40502, 1, 2, newDateTime(2013, 02, 20), "11:00"),
           newInputData( 42501, 1, 3, newDateTime(2013, 02, 23), "10:00"),
           newInputData( 42502, 1, 3, newDateTime(2013, 02, 23), "10:30"),
           newInputData( 45001, 2, 3, newDateTime(2013, 02, 21), "13:00"),
           newInputData( 45002, 2, 4, newDateTime(2013, 02, 22), "15:30"),
           newInputData( 47001, 2, 5, newDateTime(2013, 02, 24), "10:00"),
           newInputData( 47002, 2, 5, newDateTime(2013, 02, 24), "10:30"),
        };

        // receive all the stored_procedures
        i = 0;
        MystaffMyStaff=newMystaff()
        {
            staff = (from staff in staffids
                     select newStaff()
                     {
                         StaffID = staff.sid,
                         BusinessID = staff.bid,
                         SlotDates = (from day in days
                                      select newSlotDate()
                                      {
                                          Date = day.ToShortDateString(),
                                          SlotDay = day.DayOfWeek.ToString(),
                                          slots = ( from result in input
                                                    where // filter Slots that fit the requirements
                                                        day == result.SlotDate
                                                        && result.StaffID == staff.sid
                                                        && result.BusinessID == staff.bid
                                                   select newTimeSlot()
                                                   {
                                                       SlotID = result.SlotID,
                                                       SlotTime = result.SlotTime
                                                   }
                                                  ).ToArray()
                                      }
                                     // filter out days that don't have free slots
                                     ).Where(x => x.slots.Length > 0).ToArray()
                     }
                    ).ToArray()
        };


        return Json(new { MyDates, MyStaff}, JsonRequestBehavior.AllowGet);

    }
}

This Action-Method would return the following JSON

{
    "MyDates":
    {
        "ndate": "2013-03-02",
        "dates": [
            {
                "date": "23.02.2013", "day": "Saturday"
            }, {
                "date": "24.02.2013", "day": "Sunday"
            }, {
                "date": "25.02.2013", "day": "Monday"
            }, {
                "date": "26.02.2013", "day": "Tuesday"
            }, {
                "date": "27.02.2013", "day": "Wednesday"
            }, {
                "date": "28.02.2013", "day": "Thursday"
            }, {
                "date": "01.03.2013", "day": "Friday"
            }],
        "pdate": "2013-02-16"
    },
    "MyStaff":
        {
            "staff": [
                {
                    "StaffID": 1,
                    "BusinessID": 3,
                    "SlotDates": [
                        {
                            "Date": "23.02.2013",
                            "SlotDay": "Saturday",
                            "slots": [
                                { "SlotID": 42501, "SlotTime": "10:00" },
                                { "SlotID": 42502, "SlotTime": "10:30" }
                            ]
                        }]
                }, {
                    "StaffID": 2,
                    "BusinessID": 5,
                    "SlotDates": [
                        {
                            "Date": "24.02.2013",
                            "SlotDay": "Sunday",
                            "slots": [
                                { "SlotID": 47001, "SlotTime": "10:00" },
                                { "SlotID": 47002, "SlotTime": "10:30" }
                            ]
                        }
                    ]
                }
            ]
        }
};

I agree, this is not exactly what you were asking for ... but have a look at it, it is actually what you want ... its just that i took the liberty to remove redundancy In JavaScript you can easily walk through the data to display it. Using helper-models like the above MyJsonClasses help you a lot in designing Json-Outputs the way you want.

Well i hope i haven't forgotten anything.

Solution 2:

first, change your controller signature into the following :

public ActionResult Index(DateTime? start, string staffID)

then, add some logic to parse the staffID into your desired result, something like this :

var ids = staffID.Split(',');
var staffIds = ids.Select(p => p.Split('-').First()).ToList();
var businessIds = ids.Select(p => p.Split('-').Last()).ToList();

after that you can do your looping based on staffIds and businessIds list above.

Post a Comment for "Mvc 4 Controler - Using Dynamic Staffid's & Businessid's Instead Of Hard Coded Ones"