Skip to main content

#AT Repository and Unit of Work Pattern - 6

we are going to see service file below,

using ContactMang.Business.DB;
using ContactMang.Business.Unit;
using DTO= ContactMangDTO;

public interface IUserService
    {
        List<DTO.LoginUser> GetAllLoginUsers();
        DTO.LoginUser GetLoginUser(string userName);
        DTO.LoginUser GetLoginUser(long userId);
        DTO.LoginUser RegisterLoginUser(DTO.LoginUser loginUser);
    }
------------------------
 public class UserService:IUserService
    {
        private MyUnitOfWork unitOfWork = new MyUnitOfWork();
        public UserService()
        { }

        public List<DTO.LoginUser> GetAllLoginUsers()
        {
            List<DTO.LoginUser> usrList=new List<DTO.LoginUser>();
            DTO.LoginUser usr;
            var useList = unitOfWork.LoginUser.GetData();
            foreach (var item in useList)
            {
                usr = new DTO.LoginUser
                {
                    Id=item.Id,
                    Name = item.Name,
                    Password = item.Password,
                    Status = item.Status
                };
                usrList.Add(usr);
            }
            return usrList;

        }

        public DTO.LoginUser GetLoginUser(long userId)
        {
            DTO.LoginUser luser=new DTO.LoginUser();
            var usr = unitOfWork.LoginUser.FindBy(c => c.Id == userId).FirstOrDefault();
            if (usr!=null)
            {
                luser = new DTO.LoginUser
                {
                    Id = usr.Id,
                    Name = usr.Name,
                    Password = usr.Password,
                    Status = usr.Status
                };
            }
            return luser;
        }

        public DTO.LoginUser GetLoginUser(string userName)
        {
            DTO.LoginUser luser = new DTO.LoginUser();
            var usr = unitOfWork.LoginUser.FindBy(c => c.Name == userName).FirstOrDefault();
            if (usr != null)
            {
                luser = new DTO.LoginUser
                {
                    Id = usr.Id,
                    Name = usr.Name,
                    Password = usr.Password,
                    Status = usr.Status
                };
            }
            return luser;
        }

        public DTO.LoginUser RegisterLoginUser(DTO.LoginUser loginUser)
        {
            LoginUser dbLoginUser = new LoginUser
            {
                Name = loginUser.Name,
                Password = loginUser.Password,
                Status = loginUser.Status
            };
       
            var res= unitOfWork.LoginUser.Insert(dbLoginUser);
            LogRegister lr = new LogRegister { UserId = res.Id ,createdAt = DateTime.Now };
            unitOfWork.LogRegister.Insert(lr);
            unitOfWork.Commit();
       
            return loginUser;

        }
    }
------------------------------------------------
 public interface IContactsService
    {
        DTO.UserContacts GetContacts(long userId);
    }
----------------------------------------------------------
 public class ContactService : IContactsService
    {
        private MyUnitOfWork _unitOfWork;
        public ContactService()
        {
            _unitOfWork = new MyUnitOfWork();
        }

        public DTO.UserContacts GetContacts(long userId)
        {
            DTO.UserContacts usrContacts = new DTO.UserContacts();
            try
            {
                usrContacts.UserId = userId;

                var contactList = _unitOfWork.ContactInfo.FindBy(v => v.UserId == userId);
                List<DTO.Contact> cList = new List<DTO.Contact>();
                if (contactList != null)
                {
                    DTO.Contact contact;
                    foreach (var cont in contactList)
                    {
                        #region contact
                        contact = new DTO.Contact
                        {
                            Id = cont.Id,
                            UserId = cont.UserId,
                            Name = cont.Name,
                            MaildId1 = cont.MaildId1,
                            MaildId2 = cont.MaildId2,
                            Mobile1 = cont.Mobile1,
                            Mobile2 = cont.Mobile2,
                            Mobile3 = cont.Mobile3,
                            Comments = cont.Comments,
                            CreatedAt = cont.CreatedAt,
                            ModifiedAt = cont.ModifiedAt
                        };
                        #endregion

                        var addressList = _unitOfWork.ContactAddress.FindBy(v => v.ContactId == cont.Id);
                        #region Address
                        DTO.Address address;
                        List<DTO.Address> aList = new List<DTO.Address>();
                        if (addressList != null)
                        {
                            foreach (var addr in addressList)
                            {
                                address = new DTO.Address
                                {
                                    id = addr.id,
                                    ContactId = addr.ContactId,
                                    Door = addr.Door,
                                    Street = addr.Street,
                                    Place = addr.Place,
                                    City = addr.City,
                                    Taluk = addr.Taluk,
                                    Distict = addr.City,
                                    State = addr.State,
                                    Country = addr.Country,
                                    Pin = addr.Pin,
                                    Landmark = addr.Landmark
                                };
                                aList.Add(address);
                                contact.AddressList = aList;
                            }
                        }
                        #endregion
                        cList.Add(contact);
                    }
                }
                usrContacts.ContactList = cList;

            }
            catch (Exception)
            {
                 
                throw;
            }

            return usrContacts;
        }
    }

Comments