Ejemplo n.º 1
0
        public async Task <DomainModel.Entities.Employee> Handle(Command request, CancellationToken cancellationToken)
        {
            // Validate employee data.
            if (request.Employee.Name == null || request.Employee.Name == "")
            {
                throw new ApplicationException("Employee name can't be empty.");
            }
            if (request.Employee.Position == null || request.Employee.Position == "")
            {
                throw new ApplicationException("Employee position can't be empty.");
            }

            var newEmployeee = new DomainModel.Entities.Employee()
            {
                EmployeeId = Guid.NewGuid(),
                Name       = request.Employee.Name,
                Position   = request.Employee.Position
            };

            newEmployeee.RecordHistory(_dateTimeService);

            await _dbContext.Employees.AddAsync(newEmployeee);

            await _dbContext.SaveChangesAsync();

            return(newEmployeee);
        }
        public async Task HandleAsync_InputIsNormalQuery_ShouldReturnProperEmployee()
        {
            var database = new EmployeeManagementDbContext(Guid.NewGuid().ToString());

            database.Add(new DomainModel.Entities.Employee()
            {
                EmployeeId = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                Name       = "TestName1",
                Position   = "TestPosition1",
            });
            database.Add(new DomainModel.Entities.Employee()
            {
                EmployeeId = Guid.Parse("00000000-0000-0000-0000-000000000001"),
                Name       = "TestName2",
                Position   = "TestPosition2",
            });
            await database.SaveChangesAsync();

            var handler = new Application.Employee.ReadEmployeeById.Handler(database);
            var query   = new Application.Employee.ReadEmployeeById.Query()
            {
                EmployeeId = Guid.Parse("00000000-0000-0000-0000-000000000001")
            };

            var result = await handler.Handle(query, CancellationToken.None);

            Assert.AreEqual(Guid.Parse("00000000-0000-0000-0000-000000000001"), result.EmployeeId);
        }
        public async Task HandleAsync_InputIsNormalCommand_ShouldRecordHistory()
        {
            var database = new EmployeeManagementDbContext(Guid.NewGuid().ToString());

            database.Add(new DomainModel.Entities.Employee()
            {
                EmployeeId = Guid.Empty,
                Name       = "TestName",
                Position   = "TestPosition"
            });
            await database.SaveChangesAsync();

            var handler     = new Application.Employee.UpdateEmployee.Handler(database, GetDateTimeServiceMock());
            var newName     = "NewTestName";
            var newPosition = "NewTestPosition";
            var command     = new Application.Employee.UpdateEmployee.Command()
            {
                EmployeeId = Guid.Empty,
                Employee   = new DomainModel.Entities.Employee()
                {
                    Name     = newName,
                    Position = newPosition
                }
            };

            await handler.Handle(command, CancellationToken.None);

            var result  = database.Employees.FirstOrDefault(e => e.EmployeeId == Guid.Empty);
            var history = result.EditHistories[0];

            Assert.AreEqual(DateTime.MinValue, history.TimeStamp);
            Assert.AreEqual(newName, history.Name);
            Assert.AreEqual(newPosition, history.Position);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Name,Gender,Email,Department,Photo")] CreateViewModel createView)
        {
            if (ModelState.IsValid)
            {
                Employee NewEmployee = new Employee
                {
                    Name       = createView.Name,
                    Gender     = createView.Gender,
                    Department = createView.Department,
                    Email      = createView.Email,
                    PhotoPath  = await FileManagerService.SaveImage(createView.Photo)
                };
                _context.Add(NewEmployee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Details), new { Id = NewEmployee.Id }));
            }
            return(View(createView));
        }
Ejemplo n.º 5
0
        public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
        {
            var employee = await _dbContext.Employees.FirstOrDefaultAsync(e => e.EmployeeId == request.EmployeeId);

            if (employee == null)
            {
                throw new ApplicationException("Cannot find an employee with the given ID.");
            }

            _dbContext.Employees.Remove(employee);

            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Ejemplo n.º 6
0
        public async Task HandleAsync_InputIsNormalData_ShouldDeletFromDatabase()
        {
            var database = new EmployeeManagementDbContext(Guid.NewGuid().ToString());

            database.Employees.Add(new DomainModel.Entities.Employee()
            {
                EmployeeId = Guid.Empty,
                Name       = "TestName",
                Position   = "TestPosition"
            });
            await database.SaveChangesAsync();

            var handler = new Application.Employee.DeleteEmployee.Handler(database);
            var command = new Application.Employee.DeleteEmployee.Command()
            {
                EmployeeId = Guid.Empty
            };

            await handler.Handle(command, CancellationToken.None);

            Assert.AreEqual(0, database.Employees.Count());
        }
Ejemplo n.º 7
0
        public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
        {
            var employee = await _dbContext.Employees.FirstOrDefaultAsync(e => e.EmployeeId == request.EmployeeId);

            // Validate employee data.
            if (request.Employee.Name == null || request.Employee.Name == "")
            {
                throw new ApplicationException("Employee name can't be empty.");
            }
            if (request.Employee.Position == null || request.Employee.Position == "")
            {
                throw new ApplicationException("Employee position can't be empty.");
            }

            employee.Name     = request.Employee.Name;
            employee.Position = request.Employee.Position;

            employee.RecordHistory(_dateTimeService);

            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }