Nibble

UI-O-Matic V2 and custom repositories

One of the most thrilling new features in UI-O-Matic v2 is the ability to plug in your own reposities to handle the crud (thanks to Matt for the idea and implementation). So by default UI-O-Matic works agains the database using PetaPoco but with this feature we can pretty much plug in any third party API.

Example

Let’s say we want to be able to create Github issues from inside the Umbraco UI, well that can be done in a couple of easy steps.

Define the Model

1 using System.ComponentModel.DataAnnotations; 2 using UIOMatic.Attributes; 3 using UIOMatic.Example.GitHubIssues.Repo; 4 using Umbraco.Core.Persistence.DatabaseAnnotations; 5 6 namespace UIOMatic.Example.GitHubIssues.Models 7 { 8 [UIOMatic(issues, Issues, Issue, 9 RepositoryType = typeof(GitHubIssuesRepo), 10 ShowOnSummaryDashboard = true)] 11 public class Issue 12 { 13 [PrimaryKeyColumn] 14 public int Id { get; set; } 15 16 [Required] 17 [UIOMaticField(IsNameField = true)] 18 [UIOMaticListViewField] 19 public string Title { get; set; } 20 21 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.Textarea)] 22 public string Body { get; set; } 23 24 public override string ToString() 25 { 26 return Title; 27 } 28 } 29 }

The model consists of a couple of properties of course if we wish that UI-O-Matic picks these up we need to mark them with the correct attributes. One thing that we also need to do is mark 1 property with the primarykeycolumn attribute , this is the property used to identify the object.

As you see on the UIOMatic attribute we setup the RepositoryType to be something custom instead of just the default.

Create the Repository

Next we need to create that custom repo type. We can inherit from the Abstract class class AbstractUIOMaticRepository<TEntity, TId> and override it’s methods, so that’s where we’ll plug in the third party api…

1 using Octokit; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using UIOMatic.Data; 6 using UIOMatic.Models; 7 8 namespace UIOMatic.Example.GitHubIssues.Repo 9 { 10 public class GitHubIssuesRepo : AbstractUIOMaticRepository<Models.Issue, int> 11 { 12 private const string RepoOwner = TimGeyssens; 13 private const string RepoName = UIOMatic; 14 15 private GitHubClient Client 16 { 17 get 18 { 19 20 var client = new GitHubClient(new ProductHeaderValue(my-cool-app)); 21 var basicAuth = new Credentials(USERNAME, PASSWORD); 22 client.Credentials = basicAuth; 23 return client; 24 } 25 } 26 27 public override Models.Issue Create(Models.Issue entity) 28 { 29 var createIssue = new NewIssue(entity.Title); 30 createIssue.Body = entity.Body; 31 32 var issue = Client.Issue.Create(RepoOwner, RepoName, createIssue).Result; 33 34 entity.Id = issue.Number; 35 36 return entity; 37 } 38 39 40 public override void Delete(int[] ids) 41 { 42 43 foreach (var id in ids) 44 { 45 var issue = Client.Issue.Get(RepoOwner, RepoName, id).Result; 46 47 var update = issue.ToUpdate(); 48 49 update.State = ItemState.Closed; 50 51 var updatedIssue = Client.Issue.Update(RepoOwner, RepoName, issue.Number, update).Result; 52 } 53 } 54 55 56 public override Models.Issue Get(int id) 57 { 58 59 var issue = Client.Issue.Get(RepoOwner, RepoName, id).Result; 60 61 return new Models.Issue 62 { 63 64 Id = issue.Number, 65 Title = issue.Title, 66 Body = issue.Body 67 }; 68 } 69 70 71 public override IEnumerable<Models.Issue> GetAll(string sortColumn = “”, string sortOrder = “”) 72 { 73 var retVal = new List<Models.Issue>(); 74 75 var issues = Client.Issue.GetAllForRepository(RepoOwner, RepoName).Result; 76 77 foreach(var issue in issues) 78 { 79 80 retVal.Add(new Models.Issue 81 { 82 Id = issue.Number, 83 Title = issue.Title 84 }); 85 86 } 87 return retVal; 88 } 89 90 public override UIOMaticPagedResult<Models.Issue> GetPaged(int pageNumber, int itemsPerPage, string searchTerm = “”, IDictionary<string, string> filters = null, string sortColumn = “”, string sortOrder = “”) 91 { 92 //used in the ListView but we aren’t using that one currently 93 throw new NotImplementedException(); 94 } 95 96 public override long GetTotalRecordCount() 97 { 98 return GetAll().Count(); 99 } 100 101 public override Models.Issue Update(Models.Issue entity) 102 { 103 104 var issue = Client.Issue.Get(RepoOwner, RepoName, entity.Id).Result.ToUpdate(); 105 106 issue.Title = entity.Title; 107 issue.Body = entity.Body; 108 109 var updatedIssue = Client.Issue.Update(RepoOwner, RepoName, entity.Id, issue).Result; 110 111 return entity; 112 } 113 114 115 } 116 }

So as you see nothing groundbreaking in here, we are just using octokit.net to do the heavy lifting and using its API to do the CRUD operations.

Result

We are now able to perform crud operations against the Github repo from within the Umbraco backoffice.

Screen Shot 2016-11-02 at 21.28.11

Screen Shot 2016-11-02 at 21.28.23

Video

Conclusion

So as you can see the sky is the limit with UI-O-Matic v2!

Leave a Reply