Skip to content

Commit 4bbc20f

Browse files
committed
Further refactoring
1 parent 2e3787f commit 4bbc20f

5 files changed

Lines changed: 27 additions & 19 deletions

File tree

RequestTests.paw

1 Byte
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
using System;
2+
using SimpleToDoService.Entities;
3+
24
namespace SimpleToDoService
35
{
46
public class ServiceError
57
{
68
public string Message { get; set; }
79
}
810
}
11+
12+
static class Extensions
13+
{
14+
public static DateTime? CheckedTargetDate(this Task task)
15+
{
16+
if (!task.TargetDate.HasValue)
17+
return null;
18+
19+
var notificationDate = task.TargetDate.Value.ToUniversalTime();
20+
21+
if (!task.TargetDateIncludeTime)
22+
return new DateTime(notificationDate.Year, notificationDate.Month, notificationDate.Day, 0, 0, 0, notificationDate.Kind);
23+
24+
return task.TargetDate;
25+
}
26+
}

src/SimpleToDoService/Common/PushNotificationHelper.cs renamed to src/SimpleToDoService/Common/PushNotificationScheduler.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99

1010
namespace SimpleToDoService.Common
1111
{
12-
public class PushNotificationHelper
12+
public class PushNotificationScheduler
1313
{
1414
private readonly IToDoRepository repository;
1515

16-
public PushNotificationHelper(IToDoRepository repository)
16+
public PushNotificationScheduler(IToDoRepository repository)
1717
{
1818
this.repository = repository;
1919
}
2020

2121
public async System.Threading.Tasks.Task SchedulePushNotification(Task task)
2222
{
23-
var currentNotification = task.PushNotifications.FirstOrDefault();//repository.PushNotifications(task).FirstOrDefault();
23+
var currentNotification = task.PushNotifications.FirstOrDefault();
24+
25+
if (currentNotification?.DueDate == task.CheckedTargetDate())
26+
return;
27+
2428
DeletePushNotification(currentNotification);
2529
await CreatePushNotification(task);
2630
}
@@ -33,7 +37,7 @@ void DeletePushNotification(PushNotification notification)
3337

3438
async System.Threading.Tasks.Task CreatePushNotification(Task task)
3539
{
36-
var notificationDate = TaskDate(task);
40+
var notificationDate = task.CheckedTargetDate();
3741
if (!notificationDate.HasValue)
3842
return;
3943

@@ -90,18 +94,5 @@ async System.Threading.Tasks.Task CreatePushNotification(Task task)
9094
catch { }
9195
#endif
9296
}
93-
94-
private static DateTime? TaskDate(Task task)
95-
{
96-
if (!task.TargetDate.HasValue)
97-
return null;
98-
99-
var notificationDate = task.TargetDate.Value.ToUniversalTime();
100-
101-
if (!task.TargetDateIncludeTime)
102-
return new DateTime(notificationDate.Year, notificationDate.Month, notificationDate.Day, 0, 0, 0, notificationDate.Kind);
103-
104-
return task.TargetDate;
105-
}
10697
}
10798
}

src/SimpleToDoService/Controllers/TasksController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public async System.Threading.Tasks.Task<IActionResult> Post([FromBody] Task ent
7171

7272
var created = repository.CreateTask(entry);
7373

74-
await new PushNotificationHelper(repository).SchedulePushNotification(created); ;
74+
await new PushNotificationScheduler(repository).SchedulePushNotification(created); ;
7575

7676
return CreatedAtRoute("GetTask", new { Uuid = created.Uuid }, created);
7777
}

src/SimpleToDoService/Database/Repositiory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public IEnumerable<User> Users()
4646
public Task Task(Guid userUuid, Guid entryUuid)
4747
{
4848
return context.Tasks.Where(o => o.User.Uuid == userUuid && o.Uuid == entryUuid)
49-
.Include(o => o.User)
5049
.Include(o => o.PushNotifications)
5150
.FirstOrDefault();
5251
}

0 commit comments

Comments
 (0)