コード例 #1
0
        //public tblEmployees Get(int id)
        //{
        //    using (Entities db = new Entities())
        //    {
        //        return db.tblEmployees.FirstOrDefault(x => x.Id == id);
        //    }
        //}

        public HttpResponseMessage Put([FromBody] tblEmployees employeeModyfied)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    tblEmployees employee = db.tblEmployees.Find(employeeModyfied.Id);
                    if (employee != null)
                    {
                        employee.FirstName = employeeModyfied.FirstName;
                        employee.LastName  = employeeModyfied.LastName;
                        employee.Gender    = employeeModyfied.Gender;
                        employee.Salary    = employeeModyfied.Salary;

                        db.Entry(employee).State = EntityState.Modified;
                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, employee));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, $"Employee with Id = {employeeModyfied.Id} not found"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
コード例 #2
0
        //public void Post([FromBody]tblEmployees employee)
        //{
        //    using (Entities db = new Entities())
        //    {
        //        db.tblEmployees.Add(employee);
        //        db.SaveChanges();
        //    }
        //}

        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    tblEmployees employee = db.tblEmployees.Find(id);
                    if (employee != null)
                    {
                        db.tblEmployees.Remove(employee);
                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, $"Employee with Id = {id} not found to delete"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
コード例 #3
0
        public IHttpActionResult UpdateEmployees(int id)
        {
            tblEmployees employee = db.tblEmployees.FirstOrDefault(e => e.ID == id);
            employee.Name = "- Update";
            db.SaveChanges();

            return Ok<string>("Update Opreation is successful");
        }
コード例 #4
0
        public int deleteEmployees(int idEmployees)
        {
            tblEmployees deleteEmployees = repEmp.FindData(e => e.ID == idEmployees);

            if (deleteEmployees != null)
            {
                if (repEmp.Delete(deleteEmployees) > 0)
                {
                    return(1);
                }
            }
            return(0);
        }
コード例 #5
0
        //public void Put([FromBody]tblEmployees employeeModyfied)
        //{
        //        using (Entities db = new Entities())
        //        {
        //            tblEmployees employee = db.tblEmployees.Find(employeeModyfied.Id);
        //            employee.FirstName = employeeModyfied.FirstName;
        //            employee.LastName = employeeModyfied.LastName;
        //            employee.Gender = employeeModyfied.Gender;
        //            employee.Salary = employeeModyfied.Salary;

        //            db.Entry(employee).State = EntityState.Modified;
        //            db.SaveChanges();
        //        }
        //}

        //https://localhost:44306/api/employee?FirstName=uri&LastName=uriTest&Gender=Male&Salary=3000 (Body Content: add header "Content-Type:application/json", add body for id parameter "1")
        // Default convention: [From Uri] - simple types, [FromBody] - complex types
        //public HttpResponseMessage Post([FromBody]int id, [FromUri]tblEmployees employee)
        public HttpResponseMessage Post([FromBody] tblEmployees employee)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    db.tblEmployees.Add(employee);
                    db.SaveChanges();

                    HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.Created, employee);
                    message.Headers.Location = new Uri($"{Request.RequestUri}/{employee.Id}");

                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
コード例 #6
0
        public decimal calculateSalary(decimal salary)
        {
            tblContracts cn  = new tblContracts();
            tblEmployees emp = new tblEmployees();

            decimal hourlySalary  = 120 * salary * 12;
            decimal monthlySalary = salary * 12;
            string  conType       = cn.ContractType;

            if (conType == "Hourly Salary Contract")
            {
                emp.AnnualSalary = hourlySalary;
            }
            else if (conType == "Monthly Salary Contract")
            {
                emp.AnnualSalary = monthlySalary;
            }

            return(salary);
        }
コード例 #7
0
        public int updateEmployees(tblEmployees tblObj)
        {
            tblEmployees update = repEmp.FindData(e => e.ID == tblObj.ID);

            if (update != null)
            {
                update.Name           = tblObj.Name;
                update.Surname        = tblObj.Surname;
                update.DateOfBirth    = tblObj.DateOfBirth;
                update.Gender         = tblObj.Gender;
                update.IdentityNumber = tblObj.IdentityNumber;
                update.AnnualSalary   = tblObj.AnnualSalary;
                update.ContractID     = tblObj.ContractID;


                if (repEmp.Update(tblObj) > 0)
                {
                    return(1);
                }
            }
            return(0);
        }
コード例 #8
0
        public HttpResponseMessage LoadEmployeeById(int id)
        //public HttpResponseMessage Get(int id)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    tblEmployees employee = db.tblEmployees.FirstOrDefault(x => x.Id == id);

                    if (employee != null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, employee));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, $"Employee with Id = {id} not found"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }