using Flawless.Abstraction; using Flawless.Server.Models; namespace Flawless.Server.Utility; public enum RepoLocktableOptResult { Expand, Success, Already, IsChild, Rejected, } public static class RepoLocktableUtility { public static RepositoryLockedFile? GetFileLockByPath(List table, string testPath) { return table.FirstOrDefault(f => WorkPath.IsPathRelated(f.Path, testPath)); } public static RepoLocktableOptResult AddFileLockOwner(List table, string testPath, AppUser owner) { var expanded = false; foreach (var entry in table) { if (!WorkPath.IsPathRelated(entry.Path, testPath)) continue; // Check is locked by others if (entry.LockDownUser != owner) return RepoLocktableOptResult.Rejected; return testPath == entry.Path ? RepoLocktableOptResult.Already : RepoLocktableOptResult.IsChild; } // Check if includes can be locked for (var i = 0; i < table.Count; i++) { var entry = table[i]; if (!WorkPath.IsPathRelated(testPath, entry.Path)) continue; if (entry.LockDownUser != owner) return RepoLocktableOptResult.Rejected; table.RemoveAt(i); i -= 1; expanded = true; } table.Add(new RepositoryLockedFile { Path = testPath, LockDownUser = owner }); return expanded ? RepoLocktableOptResult.Expand : RepoLocktableOptResult.Success; } public static RepoLocktableOptResult RemoveFileLockOwner(List table, string testPath, AppUser? owner) { for (var i = 0; i < table.Count; i++) { var entry = table[i]; if (!WorkPath.IsPathRelated(entry.Path, testPath)) continue; if (owner != null && entry.LockDownUser != owner) return RepoLocktableOptResult.Rejected; if (testPath != entry.Path) return RepoLocktableOptResult.IsChild; table.Remove(entry); return RepoLocktableOptResult.Success; } return RepoLocktableOptResult.Rejected; } }