Example #1
0
        private async Task <int> IndexEmployee(check_yo_self_api.Server.Entities.Employee employee)
        {
            // Delete the existing entry
            try
            {
                await _indexerClient.DeleteAsync(employee.EmployeeId);
            }
            catch
            {
                // We don't care of the delete fails.
            }

            var clientEmployee = employee.Adapt <check_yo_self_indexer_client.Employee>();
            var clientList     = new List <check_yo_self_indexer_client.Employee>()
            {
                clientEmployee
            };

            try
            {
                await _indexerClient.BulkPostAsync(clientList);

                return(0);
            }
            catch (SwaggerException swaggerException)
            {
                return(swaggerException.StatusCode);
            }
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] check_yo_self_api.Server.Entities.Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            else
            {
                try
                {
                    _context.Employees.Add(employee);
                    await _context.SaveChangesAsync();

                    // Index the newly-added employee
                    int result = await IndexEmployee(employee);

                    if (result == 0)
                    {
                        return(Created("/api/Employees", employee));
                    }
                    else
                    {
                        return(StatusCode(result));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(1, ex, "Unable to add new employee: " + employee.LastName + ", " + employee.FirstName);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
        }
Example #3
0
        public async Task <IActionResult> Update(int employeeId, [FromBody] check_yo_self_api.Server.Entities.Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            else
            {
                try
                {
                    if (employeeId != employee.EmployeeId)
                    {
                        return(BadRequest());
                    }

                    var foundEmployee = _context.Employees.Find(employeeId);

                    if (foundEmployee == null)
                    {
                        return(NotFound());
                    }

                    foundEmployee.EmployeeId        = employee.EmployeeId;
                    foundEmployee.FirstName         = employee.FirstName;
                    foundEmployee.LastName          = employee.LastName;
                    foundEmployee.Salary            = employee.Salary;
                    foundEmployee.FirstPaycheckDate = employee.FirstPaycheckDate;

                    _context.Entry(foundEmployee).State = EntityState.Modified;

                    await _context.SaveChangesAsync();

                    // Reindex the updated item
                    int result = await IndexEmployee(employee);

                    if (result == 0)
                    {
                        return(NoContent());
                    }
                    else
                    {
                        return(StatusCode(result));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(1, ex, "Unable to update the specified employee with id: " + employee.EmployeeId);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
        }