using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Neo.Afx.ComponentModel;
using Neo.Afx.Services.Comments.Entities;
namespace Neo.Afx.Services.Comments
{
///
/// Comments database context container
///
public class CommentsDatabase : Database
{
///
/// Initializes a new instance of the class.
///
public CommentsDatabase()
: base(new CommentContext())
{
}
#region UserName
///
/// Gets the name of the user.
///
///
/// The name of the user.
///
public string UserName
{
get
{
if(HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
return HttpContext.Current.User.Identity.Name.ToLower();
}
return "";
}
}
#endregion
#region Create
///
/// Creates the specified entity ID.
///
/// The entity ID.
/// The item ID.
/// The comment.
///
public KeyValuePair Create(int entityID, long itemID, string comment)
{
KeyValuePair returnResult;
try
{
var result = Context.Pr_CommentCreate(entityID, itemID, comment, UserName).SingleOrDefault();
returnResult = result.HasValue ? new KeyValuePair(result.Value, (result.Value != -1) ? String.Empty : "Unknown error occured") : new KeyValuePair(-1, "Unknown error occured");
}
catch(Exception ex)
{
returnResult = new KeyValuePair(-1, ex.InnerException.Message);
}
return returnResult;
}
#endregion
#region Update
///
/// Updates the specified comment ID.
///
/// The comment ID.
/// The comment.
///
public string Update(long commentID, string comment)
{
string error;
try
{
var result = Context.Pr_CommentUpdate(commentID, comment, UserName).SingleOrDefault();
if(result.HasValue)
{
error = (result.Value) ? String.Empty : "Unknown error occured";
}
else
{
error = "Unknown error occured";
}
}
catch(Exception ex)
{
error = ex.InnerException.Message;
}
return error;
}
#endregion
#region Delete
///
/// Deletes the specified comment ID.
///
/// The comment ID.
///
public string Delete(long commentID)
{
string error;
try
{
var result = Context.Pr_CommentDelete(commentID, UserName).SingleOrDefault();
if(result.HasValue)
{
error = (result.Value) ? String.Empty : "Unknown error occured";
}
else
{
error = "Unknown error occured";
}
}
catch(Exception ex)
{
error = ex.InnerException.Message;
}
return error;
}
#endregion
#region GetComment
///
/// Gets the comment.
///
/// The comment ID.
///
public Comment GetComment(long commentID)
{
return Context.Comments.Include("UserCreatedBy").Where(a => a.CommentID == commentID && a.IsCancelled == false).FirstOrDefault();
}
#endregion
#region GetComments
///
/// Gets the comments.
///
/// The entity ID.
/// The item ID.
///
public IEnumerable GetComments(int entityID, long itemID)
{
return from q in Context.Comments.Include("UserCreatedBy")
where q.EntityID == entityID && q.ItemID == itemID && !q.IsCancelled
select q;
}
#endregion
}
}