Tuesday, 21 February 2017

Service

using BusinessEntities;
using DataModel.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace BusinessServices
{
    public class EducationCategoryService : IEducationCategoryService
    {
        private readonly UnitOfWork _unitOfWork;

        /// Public constructor.
        public EducationCategoryService()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// Fetches EducationCategory Details by ID
        public EducationCategory GetEducationCategoryById(int EducationCategory_Id)
        {
            var educationcategory = _unitOfWork.EducationCategoryRepository.GetByID(EducationCategory_Id);
            if (educationcategory != null)
            {
                //Mapper.CreateMap<City, City>();
                //var cityModel = Mapper.Map<City, City>(city);
                //return cityModel;
                return educationcategory;
            }
            return null;
        }

        /// Fetches all the EducationCategory.
        public IEnumerable<EducationCategory> GetAllEducationCategory()
        {
            var educationcategory = _unitOfWork.EducationCategoryRepository.GetAll().ToList();
            if (educationcategory.Any())
            {
                //Mapper.CreateMap<City, City>();
                //var productsModel = Mapper.Map<List<City>, List<City>>(citys);
                //return productsModel;
                return educationcategory;
            }
            return null;
        }

        /// Creates a EducationCategory
        public int CreateEducationCategory(EducationCategory educationCategoryEntity)
        {
            using (var scope = new TransactionScope())
            {
                var educationcategory = new EducationCategory
                {
                    EducationCategory_Id = educationCategoryEntity.EducationCategory_Id,
                    EducationCategory_Code = educationCategoryEntity.EducationCategory_Code,
                    EducationCategory_Name = educationCategoryEntity.EducationCategory_Name,
                    EducationCategory_Description = educationCategoryEntity.EducationCategory_Description,
                    InsertedBy = educationCategoryEntity.InsertedBy,
                    InsertedOn = educationCategoryEntity.InsertedOn,
                    IsActive = educationCategoryEntity.IsActive,
                    IsDelete = educationCategoryEntity.IsDelete

                };
                _unitOfWork.EducationCategoryRepository.Insert(educationcategory);
                _unitOfWork.Save();
                scope.Complete();
                return educationcategory.EducationCategory_Id;
            }
        }

        /// Updates a EducationCategory
        public bool UpdateEducationCategory(int EducationCategory_Id, EducationCategory educationcategoryEntity)
        {
            var success = false;
            if (educationcategoryEntity != null)
            {
                using (var scope = new TransactionScope())
                {
                    var educationcategory = _unitOfWork.EducationCategoryRepository.GetByID(EducationCategory_Id);
                    if (educationcategory != null)
                    {
                        educationcategory.EducationCategory_Id = EducationCategory_Id;
                        educationcategory.EducationCategory_Code = educationcategoryEntity.EducationCategory_Code;
                        educationcategory.EducationCategory_Name = educationcategoryEntity.EducationCategory_Name;
                        educationcategory.EducationCategory_Description = educationcategoryEntity.EducationCategory_Description;
                        educationcategory.LastModifiedBy = educationcategoryEntity.LastModifiedBy;
                        educationcategory.LastModifiedOn = educationcategoryEntity.LastModifiedOn;
                        educationcategory.IsActive = educationcategoryEntity.IsActive;
                        educationcategory.IsDelete = educationcategoryEntity.IsDelete;
                        _unitOfWork.EducationCategoryRepository.Update(educationcategory);
                        _unitOfWork.Save();
                        scope.Complete();
                        success = true;
                    }
                }
            }
            return success;
        }

        /// Deletes a particular EducationCategory
        public bool DeleteEducationCategory(int EducationCategory_Id, EducationCategory educationcategoryEntity)
        {
            var success = false;
            if (EducationCategory_Id > 0)
            {
                using (var scope = new TransactionScope())
                {
                    var educationcategory = _unitOfWork.EducationCategoryRepository.GetByID(EducationCategory_Id);
                    if (educationcategory != null)
                    {
                        educationcategory.EducationCategory_Id = EducationCategory_Id;
                        educationcategory.LastModifiedOn = educationcategoryEntity.LastModifiedOn;
                        educationcategory.LastModifiedBy = educationcategoryEntity.LastModifiedBy;
                        educationcategory.IsActive = educationcategoryEntity.IsActive;
                        educationcategory.IsDelete = educationcategoryEntity.IsDelete;
                        _unitOfWork.EducationCategoryRepository.Update(educationcategory);
                        _unitOfWork.Save();
                        scope.Complete();
                        success = true;
                    }
                }
            }
            return success;
        }
    }  
}

No comments:

Post a Comment