Skip to content

Commit 233e8d1

Browse files
Updated to ASP.NET core 6
1 parent 9af9072 commit 233e8d1

File tree

91 files changed

+74453
-193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+74453
-193
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Implementing backend for dhtmlxGantt using ASP.NET Core and Entity Framework Cor
44

55
## Requirements
66

7-
- Visual Studio 2017
7+
- Visual Studio 2022
88

99

1010
## Tutorial

SchedulerApp.sln

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28307.106
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32014.148
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchedulerApp", "SchedulerApp\SchedulerApp.csproj", "{13C3D3AE-4665-49E4-80CD-BC009D1ED68D}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchedulerApp", "SchedulerApp\SchedulerApp.csproj", "{B741246B-2402-4BF6-9FDC-BF9111822A60}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
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
14+
{B741246B-2402-4BF6-9FDC-BF9111822A60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B741246B-2402-4BF6-9FDC-BF9111822A60}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B741246B-2402-4BF6-9FDC-BF9111822A60}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B741246B-2402-4BF6-9FDC-BF9111822A60}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {CFA6CFCC-50E3-4A8E-A8AF-577FB7AB4977}
23+
SolutionGuid = {840EC6D6-244C-4C4C-9100-93CD3708CA58}
2424
EndGlobalSection
2525
EndGlobal

SchedulerApp/Controllers/EventsController.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
52
using SchedulerApp.Models;
63

74
namespace SchedulerApp.Controllers
@@ -28,9 +25,9 @@ public IEnumerable<WebAPIEvent> Get([FromQuery] DateTime from, [FromQuery] DateT
2825

2926
// GET api/events/5
3027
[HttpGet("{id}")]
31-
public WebAPIEvent Get(int id)
28+
public SchedulerEvent? Get(int id)
3229
{
33-
return (WebAPIEvent)_context
30+
return _context
3431
.Events
3532
.Find(id);
3633
}
@@ -48,18 +45,26 @@ public ObjectResult Post([FromForm] WebAPIEvent apiEvent)
4845
{
4946
tid = newEvent.Id,
5047
action = "inserted"
48+
5149
});
5250
}
5351

5452
// PUT api/events/5
5553
[HttpPut("{id}")]
56-
public ObjectResult Put(int id, [FromForm] WebAPIEvent apiEvent)
54+
public ObjectResult? Put(int id, [FromForm] WebAPIEvent apiEvent)
5755
{
5856
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;
57+
var dbEvent = _context.Events.Find(id);
58+
59+
if(dbEvent == null)
60+
{
61+
return null;
62+
}
63+
64+
dbEvent.Name = updatedEvent.Name;
65+
dbEvent.StartDate = updatedEvent.StartDate;
66+
dbEvent.EndDate = updatedEvent.EndDate;
67+
6368
_context.SaveChanges();
6469

6570
return Ok(new
@@ -75,15 +80,15 @@ public ObjectResult DeleteEvent(int id)
7580
var e = _context.Events.Find(id);
7681
if (e != null)
7782
{
78-
_context.Events.Remove(e);
79-
_context.SaveChanges();
83+
_context.Events.Remove(e);
84+
_context.SaveChanges();
8085
}
81-
86+
8287
return Ok(new
8388
{
8489
action = "deleted"
8590
});
8691
}
87-
92+
8893
}
8994
}

SchedulerApp/Controllers/RecurringEventsController.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
52
using SchedulerApp.Models;
63

74
namespace SchedulerApp.Controllers
@@ -28,9 +25,9 @@ public IEnumerable<WebAPIRecurring> Get([FromQuery] DateTime from, [FromQuery] D
2825

2926
// GET api/recurringevents/5
3027
[HttpGet("{id}")]
31-
public WebAPIRecurring Get(int id)
28+
public SchedulerRecurringEvent? Get(int id)
3229
{
33-
return (WebAPIRecurring)_context
30+
return _context
3431
.RecurringEvents
3532
.Find(id);
3633
}
@@ -46,7 +43,7 @@ public ObjectResult Post([FromForm] WebAPIRecurring apiEvent)
4643

4744
// delete a single occurrence from recurring series
4845
var resultAction = "inserted";
49-
if(newEvent.RecType == "none")
46+
if (newEvent.RecType == "none")
5047
{
5148
resultAction = "deleted";
5249
}
@@ -63,13 +60,19 @@ public ObjectResult Post([FromForm] WebAPIRecurring apiEvent)
6360
public ObjectResult Put(int id, [FromForm] WebAPIRecurring apiEvent)
6461
{
6562
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;
63+
var dbEvent = _context.RecurringEvents.Find(id);
64+
65+
if (dbEvent == null)
66+
{
67+
return null;
68+
}
69+
70+
dbEvent.Name = updatedEvent.Name;
71+
dbEvent.StartDate = updatedEvent.StartDate;
72+
dbEvent.EndDate = updatedEvent.EndDate;
73+
dbEvent.EventPID = updatedEvent.EventPID;
74+
dbEvent.RecType = updatedEvent.RecType;
75+
dbEvent.EventLength = updatedEvent.EventLength;
7376

7477
if (!string.IsNullOrEmpty(updatedEvent.RecType) && updatedEvent.RecType != "none")
7578
{
@@ -123,15 +126,15 @@ public ObjectResult DeleteEvent(int id)
123126
_context.RecurringEvents.Remove(e);
124127
}
125128

126-
129+
127130
_context.SaveChanges();
128131
}
129-
132+
130133
return Ok(new
131134
{
132135
action = "deleted"
133136
});
134137
}
135-
138+
136139
}
137140
}

SchedulerApp/Models/SchedulerContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ public SchedulerContext(DbContextOptions<SchedulerContext> options)
88
: base(options)
99
{
1010
}
11-
public DbSet<SchedulerEvent> Events { get; set; }
12-
public DbSet<SchedulerRecurringEvent> RecurringEvents { get; set; }
11+
public DbSet<SchedulerEvent> Events { get; set; } = null!;
12+
public DbSet<SchedulerRecurringEvent> RecurringEvents { get; set; } = null!;
13+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
14+
{
15+
var configuration = new ConfigurationBuilder()
16+
.SetBasePath(Directory.GetCurrentDirectory())
17+
.AddJsonFile("appsettings.json")
18+
.Build();
19+
20+
var connectionString = configuration.GetConnectionString("DefaultConnection");
21+
optionsBuilder.UseSqlServer(connectionString);
22+
}
1323
}
1424
}

SchedulerApp/Models/SchedulerEvent.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System;
2-
3-
namespace SchedulerApp.Models
1+
namespace SchedulerApp.Models
42
{
53
public class SchedulerEvent
64
{
75
public int Id { get; set; }
8-
public string Name { get; set; }
6+
public string? Name { get; set; }
97
public DateTime StartDate { get; set; }
108
public DateTime EndDate { get; set; }
119
}
12-
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.AspNetCore.Hosting;
3-
4-
namespace SchedulerApp.Models
1+
namespace SchedulerApp.Models
52
{
63
public static class SchedulerInitializerExtension
74
{
8-
public static IWebHost InitializeDatabase(this IWebHost webHost)
5+
public static IHost InitializeDatabase(this IHost webHost)
96
{
107
var serviceScopeFactory =
11-
(IServiceScopeFactory)webHost.Services.GetService(typeof(IServiceScopeFactory));
8+
(IServiceScopeFactory?)webHost.Services.GetService(typeof(IServiceScopeFactory));
129

13-
using (var scope = serviceScopeFactory.CreateScope())
10+
using (var scope = serviceScopeFactory!.CreateScope())
1411
{
1512
var services = scope.ServiceProvider;
1613
var dbContext = services.GetRequiredService<SchedulerContext>();
@@ -22,4 +19,4 @@ public static IWebHost InitializeDatabase(this IWebHost webHost)
2219
return webHost;
2320
}
2421
}
25-
}
22+
}

SchedulerApp/Models/SchedulerRecurringEvent.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
using System;
2-
3-
namespace SchedulerApp.Models
1+
namespace SchedulerApp.Models
42
{
53
public class SchedulerRecurringEvent
64
{
75
public int Id { get; set; }
8-
public string Name { get; set; }
6+
public string? Name { get; set; }
97
public DateTime StartDate { get; set; }
108
public DateTime EndDate { get; set; }
11-
129
public int EventPID { get; set; }
13-
public string RecType { get; set; }
10+
public string? RecType { get; set; }
1411
public long EventLength { get; set; }
1512

1613
}

SchedulerApp/Models/SchedulerSeeder.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
5-
namespace SchedulerApp.Models
1+
namespace SchedulerApp.Models
62
{
73
public static class SchedulerSeeder
84
{
@@ -39,4 +35,4 @@ public static void Seed(SchedulerContext context)
3935
context.SaveChanges();
4036
}
4137
}
42-
}
38+
}

SchedulerApp/Models/WebAPIEvent.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
using System;
2-
using System.Text.Encodings.Web;
1+
using System.Text.Encodings.Web;
32

43
namespace SchedulerApp.Models
54
{
65
public class WebAPIEvent
76
{
87
public int id { get; set; }
9-
public string text { get; set; }
10-
public string start_date { get; set; }
11-
public string end_date { get; set; }
8+
public string? text { get; set; }
9+
public string? start_date { get; set; }
10+
public string? end_date { get; set; }
1211

1312
public static explicit operator WebAPIEvent(SchedulerEvent ev)
1413
{
1514
return new WebAPIEvent
1615
{
1716
id = ev.Id,
18-
text = HtmlEncoder.Default.Encode(ev.Name),
17+
text = HtmlEncoder.Default.Encode(ev.Name != null ? ev.Name : ""),
1918
start_date = ev.StartDate.ToString("yyyy-MM-dd HH:mm"),
20-
end_date = ev.EndDate.ToString("yyyy-MM-dd HH:mm")
19+
end_date = ev.EndDate.ToString("yyyy-MM-dd HH:mm"),
2120
};
2221
}
2322

@@ -27,11 +26,11 @@ public static explicit operator SchedulerEvent(WebAPIEvent ev)
2726
{
2827
Id = ev.id,
2928
Name = ev.text,
30-
StartDate = DateTime.Parse(ev.start_date,
31-
System.Globalization.CultureInfo.InvariantCulture),
32-
EndDate = DateTime.Parse(ev.end_date,
33-
System.Globalization.CultureInfo.InvariantCulture)
29+
StartDate = ev.start_date != null ? DateTime.Parse(ev.start_date,
30+
System.Globalization.CultureInfo.InvariantCulture) : new DateTime(),
31+
EndDate = ev.end_date != null ? DateTime.Parse(ev.end_date,
32+
System.Globalization.CultureInfo.InvariantCulture) : new DateTime(),
3433
};
3534
}
3635
}
37-
}
36+
}

SchedulerApp/Models/WebAPIRecurring.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
using System;
2-
using System.Text.Encodings.Web;
1+
using System.Text.Encodings.Web;
32

43
namespace SchedulerApp.Models
54
{
65
public class WebAPIRecurring
76
{
87
public int id { get; set; }
9-
public string text { get; set; }
10-
public string start_date { get; set; }
11-
public string end_date { get; set; }
8+
public string? text { get; set; }
9+
public string? start_date { get; set; }
10+
public string? end_date { get; set; }
1211

1312
public int? event_pid { get; set; }
14-
public string rec_type { get; set; }
13+
public string? rec_type { get; set; }
1514
public long? event_length { get; set; }
1615

1716
public static explicit operator WebAPIRecurring(SchedulerRecurringEvent ev)
1817
{
1918
return new WebAPIRecurring
2019
{
2120
id = ev.Id,
22-
text = HtmlEncoder.Default.Encode(ev.Name),
21+
text = HtmlEncoder.Default.Encode(ev.Name != null ? ev.Name : ""),
2322
start_date = ev.StartDate.ToString("yyyy-MM-dd HH:mm"),
2423
end_date = ev.EndDate.ToString("yyyy-MM-dd HH:mm"),
25-
event_pid = ev.EventPID,
24+
25+
event_pid = ev.EventPID,
2626
rec_type = ev.RecType,
2727
event_length = ev.EventLength
2828
};
@@ -34,11 +34,12 @@ public static explicit operator SchedulerRecurringEvent(WebAPIRecurring ev)
3434
{
3535
Id = ev.id,
3636
Name = ev.text,
37-
StartDate = DateTime.Parse(ev.start_date,
38-
System.Globalization.CultureInfo.InvariantCulture),
39-
EndDate = DateTime.Parse(ev.end_date,
40-
System.Globalization.CultureInfo.InvariantCulture),
41-
EventPID = ev.event_pid != null ? ev.event_pid.Value : 0,
37+
StartDate = ev.start_date != null ? DateTime.Parse(ev.start_date,
38+
System.Globalization.CultureInfo.InvariantCulture) : new DateTime(),
39+
EndDate = ev.end_date != null ? DateTime.Parse(ev.end_date,
40+
System.Globalization.CultureInfo.InvariantCulture) : new DateTime(),
41+
42+
EventPID = ev.event_pid != null ? ev.event_pid.Value : 0,
4243
EventLength = ev.event_length != null ? ev.event_length.Value : 0,
4344
RecType = ev.rec_type
4445
};

0 commit comments

Comments
 (0)