Skip to content

Commit abdef64

Browse files
committed
Merge branch 'sync'
2 parents a89814e + 69eac28 commit abdef64

3 files changed

Lines changed: 85 additions & 17 deletions

File tree

RequestTests.paw

2.41 KB
Binary file not shown.

src/SimpleToDoService/Common/Common.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
24
using SimpleToDoService.Entities;
35

46
namespace SimpleToDoService
57
{
8+
public class BatchUpdateInstruction
9+
{
10+
[Required]
11+
public IEnumerable<Task> ToUpdate { get; set; }
12+
[Required]
13+
public IEnumerable<Guid> ToDelete { get; set; }
14+
[Required]
15+
public IEnumerable<Task> ToCreate { get; set; }
16+
}
17+
618
public class ServiceError
719
{
820
public string Message { get; set; }

src/SimpleToDoService/Controllers/TasksController.cs

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ public async System.Threading.Tasks.Task<IActionResult> Post([FromBody] Task ent
6767
if (entry == null)
6868
return BadRequest();
6969

70-
entry.UserUuid = CurrentUserUuid;
71-
72-
var created = repository.CreateTask(entry);
73-
74-
await new PushNotificationScheduler(repository).SchedulePushNotifications(created);
70+
var created = await CreateTask(entry);
7571

7672
return CreatedAtRoute("GetTask", new { Uuid = created.Uuid }, created);
7773
}
@@ -86,17 +82,12 @@ public async System.Threading.Tasks.Task<IActionResult> Put(Guid? uuid, [FromBod
8682
return BadRequest(ModelState);
8783

8884
entry.Uuid = uuid.Value;
89-
entry.UserUuid = CurrentUserUuid;
9085

91-
var updated = repository.UpdateTask(entry);
86+
var updated = await UpdateTask(entry);
9287

9388
if (updated == null)
9489
return NotFound(entry);
9590

96-
var reloaded = repository.Task(CurrentUserUuid, entry.Uuid);
97-
98-
await new PushNotificationScheduler(repository).SchedulePushNotifications(reloaded);
99-
10091
return Ok(updated);
10192
}
10293

@@ -106,14 +97,11 @@ public async System.Threading.Tasks.Task<IActionResult> Delete(Guid? uuid)
10697
if (!uuid.HasValue)
10798
return BadRequest(new ServiceError() { Message = "Object Uuid not specified" });
10899

109-
var toDelete = repository.Task(CurrentUserUuid, uuid.Value);
110-
if(toDelete == null)
111-
return new NotFoundResult();
100+
var deleted = await DeleteTask(uuid.Value);
112101

113-
toDelete.TargetDate = null;
114-
await new PushNotificationScheduler(repository).SchedulePushNotifications(toDelete);
102+
if (!deleted)
103+
return new NotFoundResult();
115104

116-
repository.DeleteTask(toDelete);
117105
return new StatusCodeResult(204);
118106
}
119107

@@ -132,5 +120,73 @@ public async System.Threading.Tasks.Task<IActionResult> ChangeCompletionStatus(G
132120

133121
return Ok(entry);
134122
}
123+
124+
[HttpPost("BatchUpdate")]
125+
public async System.Threading.Tasks.Task<IActionResult> BatchUpdate([FromBody] BatchUpdateInstruction instruction) {
126+
if (!ModelState.IsValid)
127+
return BadRequest(ModelState);
128+
129+
foreach(var task in instruction.ToCreate)
130+
{
131+
await CreateTask(task);
132+
}
133+
134+
foreach(var task in instruction.ToUpdate)
135+
{
136+
await UpdateTask(task);
137+
}
138+
139+
foreach(var uuid in instruction.ToDelete)
140+
{
141+
await DeleteTask(uuid);
142+
}
143+
144+
return Ok(Get(null));
145+
}
146+
147+
private async System.Threading.Tasks.Task<Task> CreateTask(Task task)
148+
{
149+
task.UserUuid = CurrentUserUuid;
150+
151+
if (!task.TargetDate.HasValue)
152+
task.TargetDateIncludeTime = false;
153+
154+
var created = repository.CreateTask(task);
155+
await new PushNotificationScheduler(repository).SchedulePushNotifications(created);
156+
return created;
157+
}
158+
159+
private async System.Threading.Tasks.Task<Task> UpdateTask(Task task)
160+
{
161+
task.UserUuid = CurrentUserUuid;
162+
163+
if (!task.TargetDate.HasValue)
164+
task.TargetDateIncludeTime = false;
165+
166+
var updated = repository.UpdateTask(task);
167+
168+
if (updated == null)
169+
return null;
170+
171+
var reloaded = repository.Task(CurrentUserUuid, task.Uuid);
172+
173+
await new PushNotificationScheduler(repository).SchedulePushNotifications(reloaded);
174+
175+
return reloaded;
176+
}
177+
178+
private async System.Threading.Tasks.Task<bool> DeleteTask(Guid uuid)
179+
{
180+
var toDelete = repository.Task(CurrentUserUuid, uuid);
181+
if (toDelete == null)
182+
return false;
183+
184+
toDelete.TargetDate = null;
185+
await new PushNotificationScheduler(repository).SchedulePushNotifications(toDelete);
186+
187+
repository.DeleteTask(toDelete);
188+
189+
return true;
190+
}
135191
}
136192
}

0 commit comments

Comments
 (0)