Created
April 27, 2019 23:08
-
-
Save nicksiscoe/b591011962c691370449a9905577b619 to your computer and use it in GitHub Desktop.
Action Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Core.Interfaces.Accessors; | |
using Core.Interfaces.Engines; | |
using Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Action = Models.Action; | |
namespace Engines | |
{ | |
public class ActionEngine : IActionEngine | |
{ | |
private readonly IActionAccessor actionAccessor; | |
private readonly IHistoryAccessor historyAccessor; | |
private readonly IUserAccessor userAccessor; | |
public ActionEngine(IActionAccessor actionAccessor, IHistoryAccessor historyAccessor, IUserAccessor userAccessor) | |
{ | |
this.actionAccessor = actionAccessor; | |
this.historyAccessor = historyAccessor; | |
this.userAccessor = userAccessor; | |
} | |
/// <summary> | |
/// Deletes an action and associated history items by ID | |
/// </summary> | |
/// <param name="id"></param> | |
/// <returns></returns> | |
public bool DeleteAction(int id) | |
{ | |
// Must get action first in order to know history IDs to delete | |
Action action = actionAccessor.GetById(id); | |
// Delete both associated history data as well as the action itself | |
bool oldHistoryDeleted = historyAccessor.DeleteById((Int32.Parse(action.OldData))); | |
bool newHistoryDeleted = historyAccessor.DeleteById((Int32.Parse(action.NewData))); | |
bool actionDeleted = actionAccessor.DeleteById(action.Id); | |
return oldHistoryDeleted && newHistoryDeleted && actionDeleted; | |
} | |
/// <summary> | |
/// Retrieves all actions and their associated history items | |
/// </summary> | |
/// <returns></returns> | |
public List<Action> GetAllActions() | |
{ | |
List<Action> actions = actionAccessor.GetAllActions(); | |
List<User> users = userAccessor.GetAllUsers(); | |
// Get history items associated with stored history IDs | |
actions.ForEach(action => | |
{ | |
action.OldData = historyAccessor.GetById(Int32.Parse(action.OldData)); | |
action.NewData = historyAccessor.GetById(Int32.Parse(action.NewData)); | |
}); | |
foreach(Action action in actions) | |
{ | |
string userEmail = users.FirstOrDefault(u => u.Id == action.UserId).Email; | |
if (userEmail != null) | |
action.UserEmail = userEmail; | |
} | |
actions.Reverse(); | |
return actions; | |
} | |
/// <summary> | |
/// Puts an action and associated history items in resource | |
/// </summary> | |
/// <param name="action"></param> | |
/// <returns></returns> | |
public Action PutAction(Action action) | |
{ | |
if (action == null || action.NewData == null || action.OldData == null) | |
{ | |
return null; | |
} | |
int oldId = historyAccessor.PutHistory(action.OldData); | |
int newId = historyAccessor.PutHistory(action.NewData); | |
// Set action history data to their resource IDs | |
action.OldData = oldId.ToString(); | |
action.NewData = newId.ToString(); | |
return actionAccessor.PutAction(action); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment