// PUT: api/Leave_Requests/5 public HttpResponseMessage Put(int id, [FromBody] Leave_Requests lrUpdated) { using (APIModelEntities entity = new APIModelEntities()) { try { var lrOriginal = entity.Leave_Requests.FirstOrDefault(Lr => Lr.Id == id); if (lrOriginal == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Leave record with Id " + id + " not found.")); } lrOriginal.StartDate = lrUpdated.StartDate; lrOriginal.EndDate = lrUpdated.EndDate; lrOriginal.Approved = lrUpdated.Approved; ///TODO: Need to write logic to manage total granted leaves and availed leaves. /// entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "Leave record Updated.")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } } }
public HttpResponseMessage GetLeavesForAMonth(int Month) { //method to get all emoloyee leaves for current month view. using (APIModelEntities entity = new APIModelEntities()) { return(Request.CreateResponse(HttpStatusCode.OK, entity.GetLeavesForAMonth(Month).ToList())); } }
public HttpResponseMessage GetLeavesForAnEmployee(int EmployeeId) { //method to get leaves for an employee view. using (APIModelEntities entity = new APIModelEntities()) { return(Request.CreateResponse(HttpStatusCode.OK, entity.GetLeavesForAnEmployee(EmployeeId).ToList())); } }
// DELETE: api/Leave_Requests/5 public IHttpActionResult Delete(int id) { using (APIModelEntities entity = new APIModelEntities()) { var lr = entity.Leave_Requests.FirstOrDefault(Lr => Lr.Id == id); if (lr == null) { return(NotFound()); } entity.Leave_Requests.Remove(lr); entity.SaveChanges(); return(Ok()); } }
// POST: api/Leave_Requests public IHttpActionResult Post([FromBody] Leave_Requests lr) { try { using (APIModelEntities entity = new APIModelEntities()) { entity.Leave_Requests.Add(lr); entity.SaveChanges(); return(Created(new Uri(Request.RequestUri + lr.Id.ToString()), lr)); } } catch (Exception Ex) { return(BadRequest(Ex.Message)); } }
// GET: api/Leave_Requests //public IEnumerable<string> Get() //{ // return new string[] { "value1", "value2" }; //} // GET: api/Leave_Requests/5 public IHttpActionResult Get(int id) { //Method will return only one selected leave. using (APIModelEntities entity = new APIModelEntities()) { Leave_Requests lr = entity.Leave_Requests.FirstOrDefault(L => L.Id == id); if (lr != null) { return(Ok(lr)); } else { return(NotFound()); } } }
public HttpResponseMessage GetAllLeaves() { //method to get all leaves for all employees view. List <CustomLeaves> customLeaves = new List <CustomLeaves>(); using (APIModelEntities entity = new APIModelEntities()) { //Get Leave table records. List <Leaf> _leaves = entity.Leaves.ToList(); //run a loop to get total approved leaves foreach (Leaf _l in _leaves) { List <Leave_Requests> LR = entity.Leave_Requests.Where(lr => lr.LeaveId == _l.Id).ToList(); //find out how many leave requests were found for this employee. if (LR.Count > 0) { //run a loop on leave Request object and populate leave months. foreach (Leave_Requests _lr in LR) { //check if leave request was approved if (_lr.Approved == "Y") { //populate months for start and end dates //for all leave requestes string monthName = Convert.ToDateTime(_lr.StartDate).ToString("MMM"); PopulateMonthName(monthName); monthName = Convert.ToDateTime(_lr.EndDate).ToString("MMM"); PopulateMonthName(monthName); } } //add this to custom leave object customLeaves.Add(PopulateCustomLeave("Leave", _l)); } else { //no leave request found //simply populate custom leave object customLeaves.Add(PopulateCustomLeave("NoLeave", _l)); } } //return list of custom object return(Request.CreateResponse(HttpStatusCode.OK, customLeaves)); } }