More on #865 TODO, Find out whats going on with the notifications and why exceptions are being thrown.

Bascailly custom notification messages are almost done
This commit is contained in:
Jamie.Rees 2017-06-14 16:30:06 +01:00
commit f193471b6c
46 changed files with 888 additions and 457 deletions

View file

@ -21,5 +21,6 @@ namespace Ombi.Store.Context
EntityEntry<T> Entry<T>(T entry) where T : class;
EntityEntry<TEntity> Attach<TEntity>(TEntity entity) where TEntity : class;
DbSet<TEntity> Set<TEntity>() where TEntity : class;
DbSet<NotificationTemplates> NotificationTemplates { get; set; }
}
}

View file

@ -1,10 +1,14 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Helpers;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public class OmbiContext : DbContext, IOmbiContext
public sealed class OmbiContext : DbContext, IOmbiContext
{
private static bool _created;
public OmbiContext()
@ -26,6 +30,9 @@ namespace Ombi.Store.Context
// Run Script
Database.ExecuteSqlCommand(file, 0);
// Add the notifcation templates
AddAllTemplates();
}
public DbSet<RequestBlobs> Requests { get; set; }
@ -33,10 +40,93 @@ namespace Ombi.Store.Context
public DbSet<User> Users { get; set; }
public DbSet<PlexContent> PlexContent { get; set; }
public DbSet<RadarrCache> RadarrCache { get; set; }
public DbSet<NotificationTemplates> NotificationTemplates { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=Ombi.db");
}
private void AddAllTemplates()
{
// Check if templates exist
var templates = NotificationTemplates.ToList();
if (templates.Any())
{
return;
}
var allAgents = Enum.GetValues(typeof(NotificationAgent)).Cast<NotificationAgent>().ToList();
var allTypes = Enum.GetValues(typeof(NotificationType)).Cast<NotificationType>().ToList();
foreach (var agent in allAgents)
{
foreach (var notificationType in allTypes)
{
var notificationToAdd = new NotificationTemplates();
switch (notificationType)
{
case NotificationType.NewRequest:
notificationToAdd = new NotificationTemplates
{
NotificationType = notificationType,
Message = "Hello! The user '{requestedUser}' has requested the {Type} '{Title}'! Please log in to approve this request. Request Date: {RequestedDate}",
Subject = "Ombi: New {Type} request for {Title}!",
Agent = agent,
};
break;
case NotificationType.Issue:
notificationToAdd = new NotificationTemplates
{
NotificationType = notificationType,
Message = "Hello! The user '{requestedUser}' has reported a new issue for the title {Title}! </br> {Issue}",
Subject = "Ombi: New issue for {Title}!",
Agent = agent,
};
break;
case NotificationType.RequestAvailable:
notificationToAdd = new NotificationTemplates
{
NotificationType = notificationType,
Message = "Hello! You requested {Title} on Ombi! This is now available! :)",
Subject = "Ombi: {Title} is now available!",
Agent = agent,
};
break;
case NotificationType.RequestApproved:
notificationToAdd = new NotificationTemplates
{
NotificationType = notificationType,
Message = "Hello! Your request for {Title} has been approved!",
Subject = "Ombi: your request has been approved",
Agent = agent,
};
break;
case NotificationType.AdminNote:
continue;
case NotificationType.Test:
continue;
case NotificationType.RequestDeclined:
notificationToAdd = new NotificationTemplates
{
//"Ombi: Your request has been declined",
//$"Hello! Your request for {model.Title} has been declined, Sorry!",
NotificationType = notificationType,
Message = "Hello! Your request for {Title} has been declined, Sorry!",
Subject = "Ombi: your request has been declined",
Agent = agent,
};
break;
case NotificationType.ItemAddedToFaultQueue:
continue;
default:
throw new ArgumentOutOfRangeException();
}
NotificationTemplates.Add(notificationToAdd);
}
}
SaveChanges();
}
}
}

View file

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations.Schema;
using Ombi.Helpers;
namespace Ombi.Store.Entities
{
[Table("NotificationTemplates")]
public class NotificationTemplates : Entity
{
public NotificationType NotificationType { get; set; }
public NotificationAgent Agent { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ombi.Helpers;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface INotificationTemplatesRepository
{
IQueryable<NotificationTemplates> All();
Task<IEnumerable<NotificationTemplates>> GetAllTemplates();
Task<IEnumerable<NotificationTemplates>> GetAllTemplates(NotificationAgent agent);
Task<NotificationTemplates> Insert(NotificationTemplates entity);
Task Update(NotificationTemplates template);
Task UpdateRange(IEnumerable<NotificationTemplates> template);
Task<NotificationTemplates> GetTemplate(NotificationAgent agent, NotificationType type);
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Helpers;
using Ombi.Store.Context;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public class NotificationTemplatesRepository : INotificationTemplatesRepository
{
public NotificationTemplatesRepository(IOmbiContext ctx)
{
Db = ctx;
}
private IOmbiContext Db { get; }
public IQueryable<NotificationTemplates> All()
{
return Db.NotificationTemplates.AsQueryable();
}
public async Task<IEnumerable<NotificationTemplates>> GetAllTemplates()
{
return await Db.NotificationTemplates.ToListAsync();
}
public async Task<IEnumerable<NotificationTemplates>> GetAllTemplates(NotificationAgent agent)
{
return await Db.NotificationTemplates.Where(x => x.Agent == agent).ToListAsync();
}
public async Task<NotificationTemplates> GetTemplate(NotificationAgent agent, NotificationType type)
{
return await Db.NotificationTemplates.FirstOrDefaultAsync(x => x.Agent == agent && x.NotificationType == type);
}
public async Task Update(NotificationTemplates template)
{
await Db.SaveChangesAsync();
}
public async Task UpdateRange(IEnumerable<NotificationTemplates> templates)
{
foreach (var t in templates)
{
Db.Attach(t);
Db.Entry(t).State = EntityState.Modified;
}
await Db.SaveChangesAsync();
}
public async Task<NotificationTemplates> Insert(NotificationTemplates entity)
{
var settings = await Db.NotificationTemplates.AddAsync(entity).ConfigureAwait(false);
await Db.SaveChangesAsync().ConfigureAwait(false);
return settings.Entity;
}
}
}

View file

@ -63,4 +63,15 @@ CREATE TABLE IF NOT EXISTS RequestHistory
RequestedDate varchar(50) NOT NULL,
RequestId INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS NotificationTemplates
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
NotificationType INTEGER NOT NULL,
Agent INTEGER NOT NULL,
Subject BLOB NULL,
Message BLOB NULL
);