Exemple #1
0
        public async Task <IActionResult> PutDepartment(int id, Department department)
        {
            if (id != department.DepartmentId)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DepartmentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutEmployee(int id, Employee employee)
        {
            if (id != employee.EmployeeId)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutJob(int id, Job job)
        {
            if (id != job.JobId)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!JobExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #4
0
        public IActionResult Update(Guid id, [FromBody] Employee employee)
        {
            if (employee == null || employee.Id != id)
            {
                return(BadRequest());
            }

            var employeeToUpdate = _context.Employees.Include(e => e.Addresses).FirstOrDefault(e => e.Id == id);

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

            employeeToUpdate.FirstName = employee.FirstName;
            employeeToUpdate.LastName  = employee.LastName;
            employeeToUpdate.Age       = employee.Age;

            foreach (Address address in employeeToUpdate.Addresses)
            {
                if (!employee.Addresses.Any(a => a.Id == address.Id))
                {
                    _context.Addresses.Remove(address);
                }
            }

            foreach (Address address in employee.Addresses)
            {
                var exisitngAddress = employeeToUpdate.Addresses
                                      .Where(c => c.Id == address.Id)
                                      .SingleOrDefault();

                if (exisitngAddress != null)
                {
                    _context.Entry(exisitngAddress).CurrentValues.SetValues(address);
                }
                else
                {
                    employeeToUpdate.Addresses.Add(address);
                }
            }

            _context.SaveChanges();
            return(new NoContentResult());
        }