Skip to content

Commit 73eab2c

Browse files
committed
initial commit
0 parents  commit 73eab2c

22 files changed

+812
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bin
2+
obj
3+
*.suo
4+
**/packages/*
5+
!**/packages/repositories.config
6+
.vs
7+
*.ldf
8+
*.mdf
9+
*.user

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# dhtmlxScheduler with ASP.NET Core
2+
3+
Implementing backend for dhtmlxGantt using ASP.NET Core and Entity Framework Core
4+
5+
## Requirements
6+
7+
- Visual Studio 2017
8+
9+
10+
## Tutorial
11+
12+
A complete tutorial can be found here https://docs.dhtmlx.com/scheduler/desktop__how_to_start.html

SchedulerApp.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.106
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchedulerApp", "SchedulerApp\SchedulerApp.csproj", "{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CFA6CFCC-50E3-4A8E-A8AF-577FB7AB4977}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.AspNetCore.Mvc;
5+
using SchedulerApp.Models;
6+
7+
namespace SchedulerApp.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class EventsController : ControllerBase
12+
{
13+
private readonly SchedulerContext _context;
14+
public EventsController(SchedulerContext context)
15+
{
16+
_context = context;
17+
}
18+
19+
// GET api/events
20+
[HttpGet]
21+
public IEnumerable<WebAPIEvent> Get([FromQuery] DateTime from, [FromQuery] DateTime to)
22+
{
23+
return _context.Events
24+
.Where(e => e.StartDate < to && e.EndDate >= from)
25+
.ToList()
26+
.Select(e => (WebAPIEvent)e);
27+
}
28+
29+
// GET api/events/5
30+
[HttpGet("{id}")]
31+
public WebAPIEvent Get(int id)
32+
{
33+
return (WebAPIEvent)_context
34+
.Events
35+
.Find(id);
36+
}
37+
38+
// POST api/events
39+
[HttpPost]
40+
public ObjectResult Post([FromForm] WebAPIEvent apiEvent)
41+
{
42+
var newEvent = (SchedulerEvent)apiEvent;
43+
44+
_context.Events.Add(newEvent);
45+
_context.SaveChanges();
46+
47+
return Ok(new
48+
{
49+
tid = newEvent.Id,
50+
action = "inserted"
51+
});
52+
}
53+
54+
// PUT api/events/5
55+
[HttpPut("{id}")]
56+
public ObjectResult Put(int id, [FromForm] WebAPIEvent apiEvent)
57+
{
58+
var updatedEvent = (SchedulerEvent)apiEvent;
59+
var dbEveht = _context.Events.Find(id);
60+
dbEveht.Name = updatedEvent.Name;
61+
dbEveht.StartDate = updatedEvent.StartDate;
62+
dbEveht.EndDate = updatedEvent.EndDate;
63+
_context.SaveChanges();
64+
65+
return Ok(new
66+
{
67+
action = "updated"
68+
});
69+
}
70+
71+
// DELETE api/events/5
72+
[HttpDelete("{id}")]
73+
public ObjectResult DeleteEvent(int id)
74+
{
75+
var e = _context.Events.Find(id);
76+
if (e != null)
77+
{
78+
_context.Events.Remove(e);
79+
_context.SaveChanges();
80+
}
81+
82+
return Ok(new
83+
{
84+
action = "deleted"
85+
});
86+
}
87+
88+
}
89+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.AspNetCore.Mvc;
5+
using SchedulerApp.Models;
6+
7+
namespace SchedulerApp.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class RecurringEventsController : ControllerBase
12+
{
13+
private readonly SchedulerContext _context;
14+
public RecurringEventsController(SchedulerContext context)
15+
{
16+
_context = context;
17+
}
18+
19+
// GET api/recurringevents
20+
[HttpGet]
21+
public IEnumerable<WebAPIRecurring> Get([FromQuery] DateTime from, [FromQuery] DateTime to)
22+
{
23+
return _context.RecurringEvents
24+
.Where(e => e.StartDate < to && e.EndDate >= from)
25+
.ToList()
26+
.Select(e => (WebAPIRecurring)e);
27+
}
28+
29+
// GET api/recurringevents/5
30+
[HttpGet("{id}")]
31+
public WebAPIRecurring Get(int id)
32+
{
33+
return (WebAPIRecurring)_context
34+
.RecurringEvents
35+
.Find(id);
36+
}
37+
38+
// POST api/recurringevents
39+
[HttpPost]
40+
public ObjectResult Post([FromForm] WebAPIRecurring apiEvent)
41+
{
42+
var newEvent = (SchedulerRecurringEvent)apiEvent;
43+
44+
_context.RecurringEvents.Add(newEvent);
45+
_context.SaveChanges();
46+
47+
// delete a single occurrence from recurring series
48+
var resultAction = "inserted";
49+
if(newEvent.RecType == "none")
50+
{
51+
resultAction = "deleted";
52+
}
53+
54+
return Ok(new
55+
{
56+
tid = newEvent.Id,
57+
action = resultAction
58+
});
59+
}
60+
61+
// PUT api/recurringevents/5
62+
[HttpPut("{id}")]
63+
public ObjectResult Put(int id, [FromForm] WebAPIRecurring apiEvent)
64+
{
65+
var updatedEvent = (SchedulerRecurringEvent)apiEvent;
66+
var dbEveht = _context.RecurringEvents.Find(id);
67+
dbEveht.Name = updatedEvent.Name;
68+
dbEveht.StartDate = updatedEvent.StartDate;
69+
dbEveht.EndDate = updatedEvent.EndDate;
70+
dbEveht.EventPID = updatedEvent.EventPID;
71+
dbEveht.RecType = updatedEvent.RecType;
72+
dbEveht.EventLength = updatedEvent.EventLength;
73+
74+
if (!string.IsNullOrEmpty(updatedEvent.RecType) && updatedEvent.RecType != "none")
75+
{
76+
// all modified occurrences must be deleted when we update recurring series
77+
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
78+
79+
_context.RecurringEvents.RemoveRange(
80+
_context.RecurringEvents.Where(e => e.EventPID == id)
81+
);
82+
}
83+
84+
_context.SaveChanges();
85+
86+
return Ok(new
87+
{
88+
action = "updated"
89+
});
90+
}
91+
92+
// DELETE api/recurringevents/5
93+
[HttpDelete("{id}")]
94+
public ObjectResult DeleteEvent(int id)
95+
{
96+
var e = _context.RecurringEvents.Find(id);
97+
if (e != null)
98+
{
99+
// some logic specific to recurring events support
100+
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
101+
102+
if (e.EventPID != default(int))
103+
{
104+
// deleting modified occurrence from recurring series
105+
// If an event with the event_pid value was deleted - it needs updating
106+
// with rec_type==none instead of deleting.
107+
108+
e.RecType = "none";
109+
}
110+
else
111+
{
112+
// if a recurring series was deleted - delete all modified occurrences of the series
113+
if (!string.IsNullOrEmpty(e.RecType) && e.RecType != "none")
114+
{
115+
// all modified occurrences must be deleted when we update recurring series
116+
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
117+
118+
_context.RecurringEvents.RemoveRange(
119+
_context.RecurringEvents.Where(ev => ev.EventPID == id)
120+
);
121+
}
122+
123+
_context.RecurringEvents.Remove(e);
124+
}
125+
126+
127+
_context.SaveChanges();
128+
}
129+
130+
return Ok(new
131+
{
132+
action = "deleted"
133+
});
134+
}
135+
136+
}
137+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace SchedulerApp.Models
4+
{
5+
public class SchedulerContext : DbContext
6+
{
7+
public SchedulerContext(DbContextOptions<SchedulerContext> options)
8+
: base(options)
9+
{
10+
}
11+
public DbSet<SchedulerEvent> Events { get; set; }
12+
public DbSet<SchedulerRecurringEvent> RecurringEvents { get; set; }
13+
}
14+
}

SchedulerApp/Models/SchedulerEvent.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace SchedulerApp.Models
4+
{
5+
public class SchedulerEvent
6+
{
7+
public int Id { get; set; }
8+
public string Name { get; set; }
9+
public DateTime StartDate { get; set; }
10+
public DateTime EndDate { get; set; }
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace SchedulerApp.Models
5+
{
6+
public static class SchedulerInitializerExtension
7+
{
8+
public static IWebHost InitializeDatabase(this IWebHost webHost)
9+
{
10+
var serviceScopeFactory =
11+
(IServiceScopeFactory)webHost.Services.GetService(typeof(IServiceScopeFactory));
12+
13+
using (var scope = serviceScopeFactory.CreateScope())
14+
{
15+
var services = scope.ServiceProvider;
16+
var dbContext = services.GetRequiredService<SchedulerContext>();
17+
dbContext.Database.EnsureDeleted();
18+
dbContext.Database.EnsureCreated();
19+
SchedulerSeeder.Seed(dbContext);
20+
}
21+
22+
return webHost;
23+
}
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace SchedulerApp.Models
4+
{
5+
public class SchedulerRecurringEvent
6+
{
7+
public int Id { get; set; }
8+
public string Name { get; set; }
9+
public DateTime StartDate { get; set; }
10+
public DateTime EndDate { get; set; }
11+
12+
public int EventPID { get; set; }
13+
public string RecType { get; set; }
14+
public long EventLength { get; set; }
15+
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SchedulerApp.Models
6+
{
7+
public static class SchedulerSeeder
8+
{
9+
public static void Seed(SchedulerContext context)
10+
{
11+
if (context.Events.Any())
12+
{
13+
return; // DB has been seeded
14+
}
15+
16+
var events = new List<SchedulerEvent>()
17+
{
18+
new SchedulerEvent
19+
{
20+
Name = "Event 1",
21+
StartDate = new DateTime(2019, 1, 15, 2, 0, 0),
22+
EndDate = new DateTime(2019, 1, 15, 4, 0, 0)
23+
},
24+
new SchedulerEvent()
25+
{
26+
Name = "Event 2",
27+
StartDate = new DateTime(2019, 1, 17, 3, 0, 0),
28+
EndDate = new DateTime(2019, 1, 17, 6, 0, 0)
29+
},
30+
new SchedulerEvent()
31+
{
32+
Name = "Multiday event",
33+
StartDate = new DateTime(2019, 1, 15, 0, 0, 0),
34+
EndDate = new DateTime(2019, 1, 20, 0, 0, 0)
35+
}
36+
};
37+
38+
events.ForEach(s => context.Events.Add(s));
39+
context.SaveChanges();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)