Beispiel #1
0
        public async Task <IHttpActionResult> CreateEmployeePayroll(EmployeePayrollDto employeePayrollDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
                //throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var employeePayroll = Mapper.Map <EmployeePayrollDto, EmployeePayroll>(employeePayrollDto);

            db.EmployeePayrolls.Add(employeePayroll);
            await db.SaveChangesAsync();

            employeePayrollDto.Id = employeePayroll.Id;
            return(Created(new Uri(Request.RequestUri + "/" + employeePayroll.Id), employeePayrollDto));//first agument:URI string,second agument:the object that we created

            //return CreatedAtRoute("DefaultApi", new { id = employeePayroll.Id }, employeePayroll);
        }
Beispiel #2
0
        public void  PutEmployeePayroll(int id, EmployeePayrollDto employeePayrollDto)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var employeePayrollInDb = db.EmployeePayrolls.SingleOrDefault(e => e.Id == id);

            if (employeePayrollInDb == null)
            {
                throw new  HttpResponseException(HttpStatusCode.NotFound);
            }

            Mapper.Map <EmployeePayrollDto, EmployeePayroll>(employeePayrollDto, employeePayrollInDb);

            db.SaveChanges();

            //try
            //{
            //    await db.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!EmployeePayrollExists(id))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return StatusCode(HttpStatusCode.NoContent);
        }