using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using BusinessObjects;

namespace DataLayer
{
public class DesktopSmsContext : ObjectContext
{
public DesktopSmsContext()
: base(“name=DesktopEntities”, “DesktopEntities”)
{

base.ContextOptions.ProxyCreationEnabled = false;
base.ContextOptions.LazyLoadingEnabled = true;

contacts = base.CreateObjectSet<Contact>();
contact_groups = base.CreateObjectSet<Contact_Group>();
 }

public ObjectSet<Contact> contacts { get; private set; }
public ObjectSet<Contact_Group> contact_groups { get; private set; }

 }
}

 /////////////////////////////

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GreatDevelopers.DesktopSms.BusinessObjects;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace GreatDevelopers.DesktopSms.DataLayer
{
public partial class ContactDB
{
/// <summary>
/// Adding Contact details
/// </summary>
/// <param name=”category”></param>
public static void Add(Contact contact)
{
Logger.Write(“inserting contact details from datalayer”, “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
context.AddObject(“Contacts”, contact);
context.SaveChanges();
}
Logger.Write(“contact details added successfully from datalayer”, “DEBUG”);
}
/// <summary>
/// Updating Contact details
/// </summary>
/// <param name=”category”></param>
public static void Update(Contact contact)
{
Logger.Write(string.Format(“Updating record of contact from datalayer for id{0}”, contact.Id), “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
Contact oldcontacts = context.contacts.First(i => i.Id == contact.Id);
context.contacts.ApplyCurrentValues(contact);
context.SaveChanges();
}
Logger.Write(string.Format(“contact record from datalayer for id{0} updated successfully”, contact.Id), “DEBUG”);
}
/// <summary>
/// deleting Contact details
/// </summary>
/// <param name=”category”></param>
public static void Delete(Contact contact)
{
Logger.Write(string.Format(“deleting record of contact from data layer for id{0}”, contact.Id), “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
Contact selctedcontacts = context.contacts.First(i => i.Id == contact.Id);
context.contacts.DeleteObject(selctedcontacts);
context.SaveChanges();
}
Logger.Write(string.Format(“contact record deleted from data layer for id{0}”, contact.Id), “DEBUG”);
}
/// <summary>
/// Fetching Contact details by their ID
/// </summary>
/// <param name=”id”></param>
/// <returns></returns>
public static Contact GetById(int id)
{
Logger.Write(string.Format(“fetching record of contact from data layer for id{0}”, id), “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
Contact contact = (from currentCall in context.contacts
where currentCall.Id == id
select currentCall).SingleOrDefault();
Logger.Write(string.Format(“record of contact fetched from data layer for id{0}”, id), “DEBUG”);
return contact;
}
}
}
}

public static List<Contact> GetAll()
{
Logger.Write(“fetching List of Contact from data layer”, “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
List<Contact> contactList = null;
contactList = context.contacts.ToList();
Logger.Write(“List of Contact fetched from data layer”, “DEBUG”);
return contactList;
}
}

/// <summary>
/// Fetching List of Contact by userId
/// </summary>
/// <param name=”userId”></param>
/// <returns></returns>
public static List<Contact> GetAllByUserId(int userId)
{
Logger.Write(string.Format(“fetching all Contact for the userId{0} from datalayer”, userId), “DEBUG”);
using (DesktopSmsContext context = new DesktopSmsContext())
{
var query = from contact in context.contacts
where contact.UserId == userId
select contact;
Logger.Write(string.Format(“Fetched all Contact for the userId{0} from datalayer”, userId), “DEBUG”);
return query.ToList();
}

}

/// <summary>
/// Check Mobile Existences
/// </summary>
/// <param name=”mobile”></param>
/// <param name=”userId”></param>
/// <returns></returns>
public static bool MobileExists(string mobile, int userId)
{
Logger.Write(string.Format(“Check existence for mobile {0} from the data layer”, mobile), “DEBUG”);
bool IsExist = false;
using (DesktopSmsContext context = new DesktopSmsContext())
{
Contact contacts = (from contact in context.contacts
where contact.UserId == userId && (contact.Mobile == mobile || contact.Mobile1 == mobile || contact.Mobile2 == mobile || contact.Mobile3 == mobile)
select contact).FirstOrDefault();
if (contacts != null)
IsExist = true;
}
return IsExist;
}

/// <summary>
/// Check Mobile Existences
/// </summary>
/// <param name=”mobile”></param>
/// <param name=”userId”></param>
/// <param name=”contactId”></param>
/// <returns></returns>
public static bool MobileExistsForContact(string mobile, int userId, int contactId)
{
Logger.Write(string.Format(“Check existence for mobile {0} and contact {1} from the datalayer”, mobile, contactId), “DEBUG”);
bool IsExist = false;
using (DesktopSmsContext context = new DesktopSmsContext())
{
Contact contacts = (from contact in context.contacts
where contact.UserId == userId && (contact.Mobile == mobile || contact.Mobile1 == mobile || contact.Mobile2 == mobile || contact.Mobile3 == mobile) && contact.Id != contactId
select contact).FirstOrDefault();
if (contacts != null)
IsExist = true;
}
return IsExist;
}

/// <summary>
/// Get Contact by mobile
/// </summary>
/// <param name=”mobile”></param>
/// <returns></returns>
public static Contact GetByMobile(string mobile, int userId)
{
Logger.Write(string.Format(“Fecthing Contact Details for mobile {0} from the data manager”, mobile), “DEBUG”);
Contact contacts = null;
using (DesktopSmsContext context = new DesktopSmsContext())
{
contacts = (from contact in context.contacts
where contact.Mobile == mobile && contact.UserId == userId
select contact).FirstOrDefault();
}
return contacts;
}

Advertisements