Files
OurUmbraco/NotificationsWeb/Services/NotificationService.cs
T

140 lines
4.4 KiB
C#
Raw Normal View History

2015-02-17 10:55:11 +01:00
using NotificationsWeb.Models;
using System;
2015-02-17 10:23:41 +01:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
2015-02-17 10:55:11 +01:00
using Umbraco.Core.Persistence;
2015-02-17 10:23:41 +01:00
namespace NotificationsWeb.Services
{
public class NotificationService
2015-02-17 10:23:41 +01:00
{
private readonly DatabaseContext _databaseContext;
2015-02-17 10:23:41 +01:00
public NotificationService(DatabaseContext dbContext)
{
if (dbContext == null) throw new ArgumentNullException("dbContext");
_databaseContext = dbContext;
2015-02-17 10:23:41 +01:00
}
public void SubscribeToForumTopic(int topicId, int memberId)
2015-02-17 11:04:50 +01:00
{
var r = _databaseContext.Database.SingleOrDefault<ForumTopicSubscriber>(
"SELECT * FROM forumtopicsubscribers WHERE topicId=@0 and memberId=@1",
topicId,
memberId);
if(r == null)
{
var rec = new ForumTopicSubscriber();
rec.MemberId = memberId;
rec.TopicId = topicId;
_databaseContext.Database.Insert(rec);
}
2015-02-17 10:55:11 +01:00
2015-02-17 11:04:50 +01:00
}
public void UnSubscribeFromForumTopic(int topicId, int memberId)
{
_databaseContext.Database.Delete<ForumTopicSubscriber>(
"Where topicId=@0 and memberId=@1",
topicId,
memberId);
2015-02-17 11:04:50 +01:00
}
public void RemoveAllTopicSubscriptions(int topicId)
{
_databaseContext.Database.Delete<ForumTopicSubscriber>(
"Where topicId=@0",
topicId);
2015-02-17 11:04:50 +01:00
}
public bool IsSubscribedToTopic(int topicId, int memberId)
2015-02-17 11:04:50 +01:00
{
return _databaseContext.Database.SingleOrDefault<ForumTopicSubscriber>(
"SELECT * FROM forumtopicsubscribers WHERE topicId=@0 and memberId=@1",
topicId,
memberId) != null;
2015-02-17 11:04:50 +01:00
}
public void SubscribeToForum(int forumId, int memberId)
2015-02-17 11:04:50 +01:00
{
var r = _databaseContext.Database.SingleOrDefault<ForumSubscriber>(
"SELECT * FROM forumsubscribers WHERE forumId=@0 and memberId=@1",
forumId,
memberId);
2015-02-17 11:04:50 +01:00
if (r == null)
{
var rec = new ForumSubscriber();
rec.MemberId = memberId;
rec.ForumId = forumId;
_databaseContext.Database.Insert(rec);
}
2015-02-17 11:04:50 +01:00
}
public void UnSubscribeFromForum(int forumId, int memberId)
{
_databaseContext.Database.Delete<ForumSubscriber>(
"Where forumId=@0 and memberId=@1",
forumId,
memberId);
2015-02-17 11:04:50 +01:00
}
public void RemoveAllForumSubscriptions(int forumId)
{
_databaseContext.Database.Delete<ForumSubscriber>(
"Where forumId=@0",
forumId);
2015-02-17 11:04:50 +01:00
}
public bool IsSubscribedToForum(int forumId, int memberId)
2015-02-17 11:04:50 +01:00
{
return _databaseContext.Database.SingleOrDefault<ForumSubscriber>(
"SELECT * FROM forumsubscribers WHERE forumId=@0 and memberId=@1",
forumId,
memberId) != null;
2015-02-17 11:04:50 +01:00
}
2015-02-17 10:55:11 +01:00
public Page<ForumSubscriber> GetForumSubscriptionsFromMember(int memberId, long take = 50, long page = 1)
{
var sql = new Sql()
.Select("*")
.From<ForumSubscriber>();
sql.Where<ForumSubscriber>(x => x.MemberId == memberId);
return _databaseContext.Database.Page<ForumSubscriber>(page, take, sql);
2015-02-17 10:55:11 +01:00
}
2015-02-23 10:58:42 +01:00
public long GetNumberOfForumSubscriptionsFromMember(int memberId)
{
return _databaseContext.Database.ExecuteScalar<long>("SELECT Count(*) FROM forumSubscribers where memberId=@0",memberId);
2015-02-23 10:58:42 +01:00
}
2015-02-17 10:55:11 +01:00
public Page<ForumTopicSubscriber> GetTopicSubscriptionsFromMember(int memberId, long take = 50, long page = 1)
{
var sql = new Sql()
.Select("*")
.From<ForumTopicSubscriber>();
sql.Where<ForumTopicSubscriber>(x => x.MemberId == memberId);
return _databaseContext.Database.Page<ForumTopicSubscriber>(page, take, sql);
2015-02-17 10:55:11 +01:00
}
2015-02-23 10:58:42 +01:00
public long GetNumberOfTopicSubscriptionsFromMember(int memberId)
{
return _databaseContext.Database.ExecuteScalar<long>("SELECT Count(*) FROM forumTopicSubscribers where memberId=@0",memberId);
2015-02-23 10:58:42 +01:00
}
2015-02-17 10:23:41 +01:00
}
}