Exemplo n.º 1
0
        public async Task <ActionResult <EmployeeDto> > AddForDepartment
            (int departmentId, EmployeeAddOrUpdateDto employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var employeeEntity = new Employee
            {
                DepartmentId = departmentId,
                Name         = employee.Name,
                BirthDate    = employee.BirthDate,
                Gender       = employee.Gender,
                No           = employee.No,
                PictureUrl   = employee.PictureUrl
            };
            var x = await _employeeRepository.AddAsync(employeeEntity);

            var dto = new EmployeeDto
            {
                Id           = x.Id,
                DepartmentId = x.DepartmentId,
                Name         = x.Name,
                BirthDate    = x.BirthDate,
                Gender       = x.Gender,
                No           = x.No,
                PictureUrl   = x.PictureUrl
            };

            return(dto);
        }
        public async Task UpdateForDepartmentAsync(int departmentId, int id, EmployeeAddOrUpdateDto employee)
        {
            var employeeJson =
                new StringContent(
                    JsonSerializer.Serialize(employee),
                    Encoding.UTF8,
                    "application/json");

            await _httpClient.PutAsync(
                $"api/department/{departmentId}/employee/{id}", employeeJson);
        }
        public async Task <EmployeeDto> AddForDepartmentAsync(int departmentId, EmployeeAddOrUpdateDto employee)
        {
            var employeeJson =
                new StringContent(
                    JsonSerializer.Serialize(employee),
                    Encoding.UTF8,
                    "application/json");

            var response = await _httpClient.PostAsync(
                $"api/department/{departmentId}/employee", employeeJson);

            if (response.IsSuccessStatusCode)
            {
                return(await JsonSerializer.DeserializeAsync <EmployeeDto>
                           (await response.Content.ReadAsStreamAsync()));
            }

            return(null);
        }
Exemplo n.º 4
0
        public async Task <ActionResult> UpdateForDepartment
            (int departmentId, int id, EmployeeAddOrUpdateDto employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var employeeEntity = new Employee
            {
                Id           = id,
                DepartmentId = departmentId,
                Name         = employee.Name,
                BirthDate    = employee.BirthDate,
                Gender       = employee.Gender,
                No           = employee.No,
                PictureUrl   = employee.PictureUrl
            };
            await _employeeRepository.UpdateAsync(employeeEntity);

            return(NoContent());
        }
        protected override async Task OnInitializedAsync()
        {
            if (!string.IsNullOrWhiteSpace(EmployeeId))
            {
                var employee = await EmployeeService.GetOneForDepartmentAsync
                                   (int.Parse(DepartmentId), int.Parse(EmployeeId));

                Employee = new EmployeeAddOrUpdateDto
                {
                    BirthDate  = employee.BirthDate,
                    Gender     = employee.Gender,
                    Name       = employee.Name,
                    No         = employee.No,
                    PictureUrl = employee.PictureUrl
                };
            }

            Departments = (await DepartmentService.GetAllAsync()).ToList();

            EmployeeDepartmentId = DepartmentId;
        }