示例#1
0
        public async Task <GetEmployeeResponse> CreateEmployee(UpsertEmployeeRequest employeeRequest)
        {
            var mapRequest = _mapper.Map <Employee>(employeeRequest);

            mapRequest.DateCreated = DateTime.UtcNow;

            var result = await _employeeRepository.CreateEmployee(mapRequest);

            return(_mapper.Map <GetEmployeeResponse>(result));
        }
示例#2
0
        public async Task UpdateEmployee(int employeeId, UpsertEmployeeRequest employeeRequest)
        {
            var existingEmployeeData = await _employeeRepository.GetEmployee(employeeId);

            var newEmployeeData = _mapper.Map <Employee>(employeeRequest);

            existingEmployeeData = _mapper.Map(newEmployeeData, existingEmployeeData);

            await _employeeRepository.UpdateEmployee(existingEmployeeData);
        }
示例#3
0
        public static async Task UpdateEmployee(int employeeId, UpsertEmployeeRequest request)
        {
            var requestMessage = new HttpRequestMessage(HttpMethod.Put, string.Concat(url, employeeId));

            var serializeRequest = JsonConvert.SerializeObject(request);

            requestMessage.Content = new StringContent(serializeRequest, Encoding.UTF8, "application/json");

            var responseMessage = await httpClient.SendAsync(requestMessage);
        }
示例#4
0
        public async Task UpsertEmployee(UpsertEmployeeRequest request)
        {
            using (var uow = await _context.CreateAsync(transactional: true))
            {
                Employee entity;
                if (request.EmployeeId.HasValue)
                {
                    entity = await uow.QueryAsync(new GetEmployeeByIdQuery(request.EmployeeId.Value));

                    if (entity == null)
                    {
                        throw new NotFoundException(nameof(Employee), request);
                    }

                    entity.Details = await uow.QueryAsync(new GetEmployeeDetailsQuery(entity.Id));
                }
                else
                {
                    entity = new Employee()
                    {
                        Name        = request.EmployeeName,
                        CreatedBy   = _currentUser.UserId ?? 1, // TODO: must throw excetion if curentuser is null
                        CreatedDate = _serverTime.Now
                    };

                    entity.Id = await uow.ExecuteAsync(new UpsertEmployeeCommand(entity));
                }

                foreach (var detail in request.EmployeeDetails)
                {
                    var entityDetail = new EmployeeDetail()
                    {
                        Id          = detail.Id,
                        Description = detail.Description,
                        EmployeeId  = entity.Id,
                        CreatedBy   = _currentUser.UserId ?? 1, // TODO: must throw excetion if curentuser is null
                        CreatedDate = _serverTime.Now
                    };

                    detail.Id = await uow.ExecuteAsync(new UpsertEmployeeDetailCommand(entityDetail));
                }

                // delete detail info
                var deleteDetails = entity.Details?.Where(d => request.EmployeeDetails?.Any(r => r.Id != d.Id) ?? false);
                if (deleteDetails?.Count() > 0)
                {
                    foreach (var detail in deleteDetails)
                    {
                        await uow.ExecuteAsync(new DeleteEmployeeDetailCommand(detail.Id));
                    }
                }

                uow.Commit();
            }
        }
        public async Task <IActionResult> PutEmployee([FromRoute] int employeeId, [FromBody] UpsertEmployeeRequest employeeRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _employeeService.UpdateEmployee(employeeId, employeeRequest);

            return(NoContent());
        }
        public async Task <IActionResult> PostEmployee([FromBody] UpsertEmployeeRequest employeeRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newEmployeeRecord = await _employeeService.CreateEmployee(employeeRequest);

            return(CreatedAtAction(nameof(GetEmployee), new { employeeId = newEmployeeRecord.Id }, newEmployeeRecord));
        }
示例#7
0
        public static async Task <GetEmployeeResponse> CreateEmployee(UpsertEmployeeRequest request)
        {
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, url);

            var serializeRequest = JsonConvert.SerializeObject(request);

            requestMessage.Content = new StringContent(serializeRequest, Encoding.UTF8, "application/json");

            var responseMessage = await httpClient.SendAsync(requestMessage);

            var responseContent = await responseMessage.Content.ReadAsStringAsync();

            var queryResult = JsonConvert.DeserializeObject <GetEmployeeResponse>(responseContent);

            return(queryResult);
        }
示例#8
0
        private void UpsertEmployee(GetEmployeeResponse request)
        {
            var upsertEmployeeRequest = new UpsertEmployeeRequest
            {
                FirstName = request.FirstName,
                LastName  = request.LastName
            };

            if (request.Id > 0)
            {
                var updateEmployee = Task.Run(() => HttpRequests.UpdateEmployee(request.Id, upsertEmployeeRequest));

                updateEmployee.Wait();
            }

            else
            {
                var createEmployee = Task.Run(() => HttpRequests.CreateEmployee(upsertEmployeeRequest));

                createEmployee.Wait();
            }
        }
        public async Task <IActionResult> Upsert([FromBody] UpsertEmployeeRequest employee)
        {
            await _service.UpsertEmployee(employee);

            return(NoContent());
        }