Skip to content

Commit c4415c7

Browse files
committed
Bot Store Changes
1 parent d8a3a40 commit c4415c7

7 files changed

+421
-6
lines changed

Controllers/ApiController.cs

+160-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ public IActionResult GetAllWorkers()
4747
using (var context = new Models.tasktDatabaseContext())
4848
{
4949
var workers = context.Workers.ToList();
50+
51+
//get pools
52+
var workerPools = context.WorkerPools.Include(f => f.PoolWorkers);
53+
54+
foreach (var pool in workerPools)
55+
{
56+
if (pool.PoolWorkers.Count == 0)
57+
continue;
58+
59+
var worker = new Worker
60+
{
61+
WorkerID = pool.WorkerPoolID,
62+
UserName = string.Concat("Pool '", pool.WorkerPoolName, "'")
63+
};
64+
65+
workers.Add(worker);
66+
67+
}
68+
69+
70+
71+
5072
return Ok(workers);
5173
}
5274

@@ -128,14 +150,17 @@ public IActionResult CheckInWorker(Guid workerID, bool engineBusy)
128150
Models.PublishedScript publishedScript = null;
129151

130152

131-
132153
if (!engineBusy)
133154
{
155+
156+
157+
134158
scheduledTask = context.Tasks.Where(f => f.WorkerID == workerID && f.Status == "Scheduled").FirstOrDefault();
135159

160+
136161
if (scheduledTask != null)
137162
{
138-
163+
//worker directly scheduled
139164

140165
publishedScript = context.PublishedScripts.Where(f => f.PublishedScriptID.ToString() == scheduledTask.Script).FirstOrDefault();
141166

@@ -151,8 +176,40 @@ public IActionResult CheckInWorker(Guid workerID, bool engineBusy)
151176

152177

153178
}
179+
else
180+
{
181+
//check if any pool tasks
182+
183+
var workerPools = context.WorkerPools
184+
.Include(f => f.PoolWorkers)
185+
.Where(f => f.PoolWorkers.Any(s => s.WorkerID == workerID)).ToList();
186+
187+
foreach (var pool in workerPools)
188+
{
189+
scheduledTask = context.Tasks.Where(f => f.WorkerID == pool.WorkerPoolID && f.Status == "Scheduled").FirstOrDefault();
190+
191+
if (scheduledTask != null)
192+
{
193+
//worker directly scheduled
194+
195+
publishedScript = context.PublishedScripts.Where(f => f.PublishedScriptID.ToString() == scheduledTask.Script).FirstOrDefault();
154196

155-
197+
if (publishedScript != null)
198+
{
199+
scheduledTask.Status = "Deployed";
200+
}
201+
else
202+
{
203+
scheduledTask.Status = "Deployment Failed";
204+
}
205+
206+
break;
207+
208+
}
209+
210+
}
211+
212+
}
156213
}
157214

158215
context.SaveChanges();
@@ -326,7 +383,7 @@ public IActionResult UpdateTask(Guid taskID, string status, Guid workerID, strin
326383
//Todo: Needs Testing
327384
using (var context = new Models.tasktDatabaseContext())
328385
{
329-
var taskToUpdate = context.Tasks.Where(f => f.TaskID == taskID && f.WorkerID == workerID).FirstOrDefault();
386+
var taskToUpdate = context.Tasks.Where(f => f.TaskID == taskID).FirstOrDefault();
330387

331388
if (status == "Completed")
332389
{
@@ -363,14 +420,29 @@ public IActionResult ScheduleTask([FromBody] NewTaskRequest request)
363420
return BadRequest();
364421
}
365422

423+
424+
//find worker
366425
var workerRecord = context.Workers.Where(f => f.WorkerID == request.workerID).FirstOrDefault();
426+
427+
//if worker wasnt found then search for pool
428+
367429
if (workerRecord == null)
368430
{
369-
return BadRequest();
431+
//find from pool
432+
var poolExists = context.WorkerPools.Any(s => s.WorkerPoolID == request.workerID);
433+
434+
//if pool wasnt found
435+
if (!poolExists)
436+
{
437+
//return bad request
438+
return BadRequest();
439+
}
370440
}
371441

442+
443+
//create new task
372444
var newTask = new Models.Task();
373-
newTask.WorkerID = workerRecord.WorkerID;
445+
newTask.WorkerID = request.workerID;
374446
newTask.TaskStarted = DateTime.Now;
375447
newTask.Status = "Scheduled";
376448
newTask.ExecutionType = "Remote";
@@ -474,6 +546,88 @@ public IActionResult GetAllAssignments()
474546

475547
}
476548

549+
[HttpPost("/api/Assignments/Add")]
550+
public IActionResult AddAssignment([FromBody] Assignment assignment)
551+
{
552+
using (var context = new Models.tasktDatabaseContext())
553+
{
554+
context.Assignments.Add(assignment);
555+
context.SaveChanges();
556+
return Ok(assignment);
557+
558+
}
559+
560+
561+
562+
}
563+
[HttpPost("/api/BotStore/Add")]
564+
public IActionResult AddDataToBotStore([FromBody] BotStoreModel storeData)
565+
{
566+
567+
using (var context = new Models.tasktDatabaseContext())
568+
{
569+
570+
if (!context.Workers.Any(f => f.WorkerID == storeData.LastUpdatedBy))
571+
{
572+
return Unauthorized();
573+
}
574+
575+
if (context.BotStore.Any(f => f.BotStoreName == storeData.BotStoreName))
576+
{
577+
var existingItem = context.BotStore.Where(f => f.BotStoreName == storeData.BotStoreName).FirstOrDefault();
578+
existingItem.BotStoreValue = storeData.BotStoreValue;
579+
existingItem.LastUpdatedOn = DateTime.Now;
580+
existingItem.LastUpdatedBy = storeData.LastUpdatedBy;
581+
}
582+
else
583+
{
584+
storeData.LastUpdatedOn = DateTime.Now;
585+
context.BotStore.Add(storeData);
586+
}
587+
588+
context.SaveChanges();
589+
return Ok(storeData);
590+
591+
}
592+
593+
594+
595+
}
596+
[HttpPost("/api/BotStore/Get")]
597+
public IActionResult GetDataBotStore([FromBody] BotStoreRequest requestData)
598+
{
599+
600+
using (var context = new Models.tasktDatabaseContext())
601+
{
602+
603+
if (!context.Workers.Any(f => f.WorkerID == requestData.workerID))
604+
{
605+
return Unauthorized();
606+
}
607+
608+
609+
var requestedItem = context.BotStore.Where(f => f.BotStoreName == requestData.BotStoreName).FirstOrDefault();
610+
611+
if (requestedItem == null)
612+
{
613+
return NotFound();
614+
}
615+
616+
switch (requestData.requestType)
617+
{
618+
case BotStoreRequest.RequestType.BotStoreValue:
619+
return Ok(requestedItem.BotStoreValue);
620+
case BotStoreRequest.RequestType.BotStoreModel:
621+
return Ok(requestedItem);
622+
default:
623+
return StatusCode(400);
624+
}
625+
626+
}
627+
628+
629+
630+
}
477631

478632

479633
}

Migrations/20190208035453_botstore.Designer.cs

+173
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)