Easily Synchronize Blazor Resource Scheduler with RESTful Services
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Synchronize Blazor Resource Scheduler with Restful Services

Easily Synchronize Blazor Resource Scheduler with RESTful Services

RESTful services have become the de facto standard for writing web applications. They help us expose resources on the internet using simple HTTP requests. REST (representational state transfer) is an architectural style that enables developers to create web services that are easy to build, read, and maintain. This approach allows the developers to focus on the business logic of their application instead of worrying about how data is transmitted over the network.

Our Syncfusion Blazor Scheduler is a fully-featured calendar component. It allows users to manage their time efficiently. It facilitates easy resource scheduling, rescheduling appointments through editor pop-ups, drag and drop, and resizing actions.

This blog explains the steps to synchronize data between the Syncfusion Blazor Scheduler and RESTful services.

Let’s get started!

Project setup

First, create a simple Blazor-hosted(WebAssembly) application. Refer to the Getting started with Blazor ASP.NET Core-hosted apps in the Visual Studio page for the introduction and project creation steps.

The project will look like in the following screenshot.

Created Blazor Server-Side ProjectNow, refer to the Adding a component package to the application page to add the Blazor Scheduler component to your application.

We are going to use Entity Framework Core and data migration commands to create an SQL database and table. So, install the Microsoft.EntityFrameworkCore and  Microsoft.EntityFrameworkCore.SqlServer NuGet packages to perform these actions in the server project.

Creating models

Let’s create the required database models for the application.

Create a new folder from the project shared directory under the name Models.

Create a new folder from the project shared directory under the name Models

Then, create an event model class named EventModel inside the Models folder. Refer to the following code example.
EventModel.cs

public class EventModel
{
    public int Id { get; set; }
    public string Subject { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }
    public string StartTimezone { get; set; }
    public string EndTimezone { get; set; }
    public string Location { get; set; }
    public string Description { get; set; }
    public bool? IsAllDay { get; set; }
    public bool? IsBlock { get; set; }
    public bool? IsReadOnly { get; set; }
    public int? FollowingID { get; set; }
    public int? RecurrenceID { get; set; }
    public string RecurrenceRule { get; set; }
    public string RecurrenceException { get; set; }
    public int? OwnerId { get; set; }
}

Now, create a resource model class named ResourceModel inside the Models folder.

ResourceModel.cs

public class ResourceModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
}

Note: Refer to the Create a data model documentation to learn more about EntityFramework data model creation.

Creating a database and context

Here, we will create an SQL database based on the models in this application. Refer to the documentation on creating the database and tables based on the models.

After creating the database, we have to add the resource table data to it to retrieve the resource initially. In this app, the local SQL database is named ScheduleData.mdf and is available under the App_Data folder for reference.

The next step is to creating a context for the database. Create a SchedulerContext class under the data folder in the server project. Refer to the following screenshot.

Create a SchedulerContext class under the data folder in the server projectIn the SchedulerContext class,  configure the database models into a database table like in the following code example.

public class SchedulerContext : DbContext
{
    public SchedulerContext(DbContextOptions<SchedulerContext> options) : base(options) { }

    public virtual DbSet<EventModel> EventModels { get; set; }
    public virtual DbSet<ResourceModel> ResourceModels { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EventModel>().ToTable("EventData");
        modelBuilder.Entity<ResourceModel>().ToTable("ResourceData");
    }
}

Thus, we have successfully created the database and its context.

Configuring the database

Next, register the SchedulerContext class in the startup class of the ConfigureServices section.

Refer to the following code.

services.AddDbContext<SchedulerContext>(context => context.UseSqlServer("<database connection string>"));

Now, we have successfully connected the app with the database.

Adding API controllers

Then, we create API controllers to perform CRUD actions with the RESTful services. Create the ScheduleController and ResourceController APIs under the Controllers folder in the server project to easily retrieve, update, and remove events data in the database.

ScheduleController.cs

[Route("api/[controller]")]
[ApiController]
public class ScheduleController : ControllerBase
{
    private readonly SchedulerContext _context;

    public ScheduleController(SchedulerContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<EventModel> Get()
    {
        return _context.EventModels.ToList();
    }

    [HttpPost]
    public void Post([FromBody] EventModel eventModel)
    {
        if (ModelState.IsValid)
        {
            try
            {
                _context.EventModels.Add(eventModel);
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
    }

    [HttpPut("{id}")]
    public void Put(int id, [FromBody] EventModel eventModel)
    {
        if (ModelState.IsValid)
        {
            try
            {
                _context.Entry(eventModel).State = EntityState.Modified;
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
    }

    [HttpDelete("{id}")]
    public void Delete(int id)
    {
        if (ModelState.IsValid)
        {
            try
            {
                EventModel eventModel = _context.EventModels.Find(id);
                _context.EventModels.Remove(eventModel);
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
    }

}

ResourceController.cs

[Route("api/[controller]")]
[ApiController]
public class ResourceController : ControllerBase
{
    private readonly SchedulerContext _context;

    public ResourceController(SchedulerContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<ResourceModel> Get()
    {
        return _context.ResourceModels.ToList();
    }
}

Note: Refer to the Add Web API Controllers documentation to synchronize data between the app and server.

Configure RESTful services into Blazor Scheduler

Finally, render the Syncfusion Blazor Scheduler component in the index.razor file in the client project with the RESTful API services. Use the Data Manager to perform data processing between the Blazor Scheduler and RESTful services.

Refer to the following example code.

Index.razor

@using BlazorScheduler.Shared.Models
@using Syncfusion.Blazor
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Schedule

<SfSchedule TValue="EventModel" Width="100%" Height="650px" CurrentView="View.Month" SelectedDate="new DateTime(2021, 9, 1)">
    <ScheduleGroup Resources="@GroupData"></ScheduleGroup>
    <ScheduleResources>
        <ScheduleResource TItem="ResourceModel" TValue="int" Field="OwnerId" Title="Choose Owner" Name="Owners" IdField="Id" TextField="Name" ColorField="Color">
            <SfDataManager Url="/api/Resource" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
        </ScheduleResource>
    </ScheduleResources>
    <ScheduleEventSettings TValue="EventModel">
        <SfDataManager Url="/api/Schedule" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
    </ScheduleEventSettings>
</SfSchedule>

@code{
    private string[] GroupData = new string[] { "Owners" };
}

After synchronizing the RESTful services with the Blazor Scheduler, we can easily retrieve data from the RESTful services even at the initial load of the Blazor Scheduler.

For every CRUD action in the Blazor Scheduler, the data will properly synchronize with the database using the RESTful services.

Synchronize Blazor Scheduler with RESTful Services
Synchronize Blazor Scheduler with RESTful Services

GitHub reference

You can check out the complete GitHub demo for Synchronizing Blazor Scheduler events and resource data with RESTful services.

Summary

Thanks for reading! This blog provided the easy steps to synchronize events and resource data in the Syncfusion Blazor Scheduler with RESTful services. This will help you integrate your app with RESTful services and enjoy its benefits. So, try out the steps in this blog post and leave your feedback in the comments section below.

Syncfusion Essential Studio for Blazor offers over 65 high-performance, lightweight, and responsive UI components for the web, including file-format libraries, in a single package. Use them to enhance your app’s look!

For existing customers, the latest version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the available features. Also, try out our other demos from this GitHub location.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Comments (1)

Hi Sellakumar

Thanks for this article, it has really helped me a lot. The only issue I am having is when I try to edit an appointment, I just get a 404 error, and I can’t establish why. Any pointers would be gratefully received.

Thanks and regards

Peter

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed