Esempio n. 1
0
 public List <xPlug.BusinessObject.Department> GetDepartments()
 {
     try
     {
         using (var db = new ExpenseManagerDBEntities())
         {
             var myObjList         = db.Departments.ToList();
             var myBusinessObjList = new List <xPlug.BusinessObject.Department>();
             if (myObjList == null)
             {
                 return(myBusinessObjList);
             }
             //Re-Map each Entity Object to Business Object
             foreach (var item in myObjList)
             {
                 var myBusinessObj = DepartmentMapper.Map <Department, xPlug.BusinessObject.Department>(item);
                 if (myBusinessObj == null)
                 {
                     continue;
                 }
                 myBusinessObjList.Add(myBusinessObj);
             }
             return(myBusinessObjList);
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(new List <xPlug.BusinessObject.Department>());
     }
 }
Esempio n. 2
0
 public xPlug.BusinessObject.Department GetDepartment(int departmentId)
 {
     try
     {
         using (var db = new ExpenseManagerDBEntities())
         {
             var myObj = db.Departments.SingleOrDefault(s => s.DepartmentId == departmentId);
             if (myObj == null)
             {
                 return(new xPlug.BusinessObject.Department());
             }
             //Re-Map Entity Object to Business Object
             var myBusinessObj = DepartmentMapper.Map <Department, xPlug.BusinessObject.Department>(myObj);
             if (myBusinessObj == null)
             {
                 return(new xPlug.BusinessObject.Department());
             }
             { return(myBusinessObj); }
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(new xPlug.BusinessObject.Department());
     }
 }
Esempio n. 3
0
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(IdNotProvidedBadRequest(logger.Here()));
            }
            Department department = await departmentService.FindDepartmentAsync(id);

            if (department == null)
            {
                return(ModelNotFound(logger.Here(), id));
            }

            //check if this department does not have any employee
            bool isEmptyDepartment = await departmentService.HasNoEmployeeAsync(department.DepartmentId);

            if (isEmptyDepartment == false)
            {
                return(ErrorResponseOk(logger.Here(), $"Delete department failed. Cannot delete department that has employees associated with it. DepartmentId: {department.DepartmentId}"));
            }

            //delete department
            db.Departments.Remove(department);
            await db.SaveChangesAsync();

            logger.Here().Information("Deleted department successfully");

            //map to delete response
            EntityDeleteResponse entityDeleteResponse = DepartmentMapper.MapFromDepartmentToEntityDeleteResponse(department);

            return(Ok(entityDeleteResponse));
        }
 public int UpdateDepartmentCheckDuplicate(BusinessObject.Department department)
 {
     try
     {
         //Re-Map Object to Entity Object
         var myEntityObj = DepartmentMapper.Map <BusinessObject.Department, Department>(department);
         if (myEntityObj == null)
         {
             return(-2);
         }
         using (var db = new ExpenseManagerDBEntities())
         {
             if (db.Departments.Count(m => m.Name.ToLower().Replace(" ", string.Empty) == department.Name.ToLower().Replace(" ", string.Empty) && m.DepartmentId != department.DepartmentId) > 0)
             {
                 return(-3);
             }
             db.Departments.Attach(myEntityObj);
             db.ObjectStateManager.ChangeObjectState(myEntityObj, EntityState.Modified);
             db.SaveChanges();
             return(1);
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(0);
     }
 }
Esempio n. 5
0
        public void testDepartmentMapper()
        {
            Department deptEntity = new Department
            {
                token          = System.Guid.NewGuid(),
                DepartmentName = "Department1",
                Address        = "Dep1-Address1",
                Employees      = new List <Employee>()
                {
                    new Employee()
                    {
                        token = System.Guid.NewGuid(), FirstName = "Dep1-FName1", LastName = "Dep1-LName1", JobTitle = "Title1", MailingAddress = "Address1"
                    },
                    new Employee()
                    {
                        token = System.Guid.NewGuid(), FirstName = "Dep1-FName2", LastName = "Dep1-LName2", JobTitle = "Title2", MailingAddress = "Address2"
                    },
                    new Employee()
                    {
                        token = System.Guid.NewGuid(), FirstName = "Dep1-FName3", LastName = "Dep1-LName3", JobTitle = "Title3", MailingAddress = "Address3"
                    },
                }
            };
            DepartmentModel deptModel   = DepartmentMapper.toModel(deptEntity);
            Department      deptEntity2 = DepartmentMapper.toEntity(new Department(), deptModel); // convert it back from Entity to Model

            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(deptEntity.ByProperties(), deptEntity2);
        }
 public List <BusinessObject.Unit> GetActiveFilteredOrderedUnits()
 {
     try
     {
         using (var db = new ExpenseManagerDBEntities())
         {
             var myObjList         = db.Units.Where(m => m.DepartmentId != 8).ToList();
             var myBusinessObjList = new List <BusinessObject.Unit>();
             if (!myObjList.Any())
             {
                 return(myBusinessObjList);
             }
             //Re-Map each Entity Object to Business Object
             foreach (var item in myObjList)
             {
                 var myBusinessObj = DepartmentMapper.Map <Unit, BusinessObject.Unit>(item);
                 if (myBusinessObj == null)
                 {
                     continue;
                 }
                 myBusinessObjList.Add(myBusinessObj);
             }
             return(myBusinessObjList.OrderBy(m => m.Name).ToList());
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(new List <BusinessObject.Unit>());
     }
 }
Esempio n. 7
0
        public async Task Update(DepartmentDto newDepartment)
        {
            var department = await this.unitOfWork.DepartmentRepository.Get(newDepartment.Id);

            DepartmentMapper.MapUpdate(department, newDepartment);

            await this.unitOfWork.DepartmentRepository.Update(department);
        }
Esempio n. 8
0
        public async Task <IActionResult> Create([FromBody] DepartmentCreateRequest requestModel)
        {
            //map to entity
            Department department = DepartmentMapper.MapFromDepartmentCreateRequestToDepartment(requestModel);

            db.Departments.Add(department);
            await db.SaveChangesAsync();

            logger.Here().Information("Created department successfully");

            return(CreatedAtRoute("GetDepartmentById", new { id = department.DepartmentId }, null));
        }
Esempio n. 9
0
        public async Task <IActionResult> GetAll()
        {
            List <Department> department = await departmentService.GetDepartments()
                                           .AsNoTracking()
                                           .ToListAsync();

            logger.Here().Information("Get departments successfully");
            //map to response
            DepartmentGetAllResponse departmentGetAllResponse = DepartmentMapper.MapFromDepartmentsToDepartmentGetAllResponse(db, department);

            return(Ok(departmentGetAllResponse));
        }
        public void should_map_department_properties()
        {
            //Given  //when
            var csvMapper = new DepartmentMapper();

            //then
            csvMapper.IndexPropertyMappings.Should().NotBeNullOrEmpty();
            csvMapper.IndexPropertyMappings.Count().Should().Be(2);
            var mappedProperty = csvMapper.IndexPropertyMappings.First();

            mappedProperty.Index.Should().Be(0);
            mappedProperty.AttributeName.Should().Be("Name");
        }
Esempio n. 11
0
        public async Task <IActionResult> Update([FromBody] DepartmentUpdateRequest requestModel)
        {
            Department department = await departmentService.FindDepartmentAsync(requestModel.Id);

            if (department == null)
            {
                return(ModelNotFound(logger.Here(), requestModel.Id));
            }

            DepartmentMapper.MapFromDepartmentUpdateRequestToDepartment(requestModel, department);
            db.Departments.Update(department);
            await db.SaveChangesAsync();

            logger.Here().Information("Updated department successfully");

            return(NoContent());
        }
 public IEnumerable <Department> GetDataUsingDataContract()
 {
     using (var c = new SqlConnection(Constants.CS))
     {
         var m = new DepartmentMapper();
         c.Open();
         using (var cmd = c.CreateCommand())
         {
             cmd.CommandText = "GetDepartments";
             cmd.CommandType = System.Data.CommandType.StoredProcedure;
             using (var r = cmd.ExecuteReader())
             {
                 return(m.Map(r));
             }
         };
     }
 }
        public async Task <IActionResult> OnPostAsync(int id)
        {
            try
            {
                var departmentToDelete = new DepartmentMapper().SingleTo(await _context.Departments.AsNoTracking().FirstOrDefaultAsync(m => m.DepartmentId == id));

                if (departmentToDelete != null)
                {
                    _context.Departments.Remove(new DepartmentMapper().SingleFrom(departmentToDelete));

                    await _context.SaveChangesAsync();
                }

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(RedirectToPage("./Delete",
                                      new { concurrencyError = true, id = id }));
            }
        }
Esempio n. 14
0
    public IEnumerable <T> Parse <T>(string fileName) where T : class, new()
    {
        var           lines  = File.ReadLines(fileName);
        CsvMapper <T> mapper = null;

        if (typeof(T) == typeof(Employee))
        {
            mapper = new EmployeeMapper() as CsvMapper <T>;
        }
        if (typeof(T) == typeof(Department))
        {
            mapper = new DepartmentMapper() as CsvMapper <T>;
        }

        if (mapper == null)
        {
            throw new MissingMappingException();
        }

        return(mapper.Map(lines));
    }
Esempio n. 15
0
 public bool UpdateDepartment(xPlug.BusinessObject.Department department)
 {
     try
     {
         //Re-Map Object to Entity Object
         var myEntityObj = DepartmentMapper.Map <xPlug.BusinessObject.Department, Department>(department);
         if (myEntityObj == null)
         {
             return(false);
         }
         using (var db = new ExpenseManagerDBEntities())
         {
             db.Departments.Attach(myEntityObj);
             db.ObjectStateManager.ChangeObjectState(myEntityObj, EntityState.Modified);
             db.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(false);
     }
 }
Esempio n. 16
0
 public int AddDepartment(xPlug.BusinessObject.Department department)
 {
     try
     {
         //Re-Map Object to Entity Object
         var myEntityObj = DepartmentMapper.Map <xPlug.BusinessObject.Department, Department>(department);
         if (myEntityObj == null)
         {
             return(-2);
         }
         using (var db = new ExpenseManagerDBEntities())
         {
             db.AddToDepartments(myEntityObj);
             db.SaveChanges();
             department.DepartmentId = myEntityObj.DepartmentId;
             return(department.DepartmentId);
         }
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         return(0);
     }
 }
Esempio n. 17
0
        public void PopulateDepartmentsDropDownList(object selectedDepartment = null)
        {
            var departmentsQuery = new DepartmentMapper().ManyTo(Context.Departments).OrderBy(d => d.Name);

            DepartmentName = new SelectList(departmentsQuery, "Id", "Name", selectedDepartment);
        }
Esempio n. 18
0
 public DepartmentService(ItCompanyContext context)
 {
     Repo   = new EfCoreDepartmentRepository(context);
     Mapper = new DepartmentMapper(Repo);
 }
Esempio n. 19
0
        public async Task <DepartmentDto> Get(int id)
        {
            var department = await this.unitOfWork.DepartmentRepository.Get(id);

            return(DepartmentMapper.Map(department));
        }
Esempio n. 20
0
        public async Task Create(DepartmentDto department)
        {
            var dep = DepartmentMapper.MapCreate(department);

            await this.unitOfWork.DepartmentRepository.Create(dep);
        }