public HttpResponseMessage Get(int barber_id) { try { // Check if a session already exists or if it's expired //if (HttpContext.Current.Session["Token"] == null) // return Request.CreateResponse(HttpStatusCode.Unauthorized, new { Success = false, Message = "Session expired! Unable to authenticate user." }); using (SalonDbEntities entities = new SalonDbEntities()) { tblbarber selectedBarber = entities.tblbarbers.FirstOrDefault(e => e.barber_id == barber_id); if (selectedBarber != null) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Barber retrieved successfully!", Barber_details = new { selectedBarber.barber_id, selectedBarber.barber_name, selectedBarber.allocated_seat_no, selectedBarber.salon_id, selectedBarber.is_available } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Barber with id = ", barber_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve barber details.")); } }
public HttpResponseMessage Post([FromBody] JObject barber_details) { try { // Check if a session already exists or if it's expired //if (HttpContext.Current.Session["Token"] == null) // return Request.CreateResponse(HttpStatusCode.Unauthorized, new { Success = false, Message = "Session expired! Unable to authenticate user." }); string barber_name = barber_details["barber_name"].ToString().Trim(); int salon_id = int.Parse(barber_details["salon_id"].ToString()); int allocated_seat_no = int.Parse(barber_details["allocated_seat_no"].ToString()); using (SalonDbEntities entities = new SalonDbEntities()) { // Check if the barber name already exists in the particular salon. Otherwise there will be confusions. if (entities.tblbarbers.Any(e => e.barber_name.ToUpper().Trim() == barber_name.ToUpper().Trim() && e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Failed to create barber! A barber with the same name already exists in salon id = " + salon_id + ". Please enter another name.")); } // Check if the user entered seat no exists in the salon // 1. Get the no of seats available in the salon // 2. Check if the entered seat no is within that range var obj = entities.tblsalons.Where(p => p.salon_id.Equals(salon_id)).Select(p => new { seating_capacity = p.seating_capacity }).FirstOrDefault(); if (1 <= allocated_seat_no && allocated_seat_no <= obj.seating_capacity) { // If the seat no exists, then check if a barber in that salon, has already been assigned to that particular seat no if (entities.tblbarbers.Any(e => e.salon_id == salon_id && e.allocated_seat_no == allocated_seat_no)) { return(Messages.GetInstance().HandleException("Failed to create barber! A barber has already been assigned to seat no = " + allocated_seat_no + " in salon id = " + salon_id + ". Please enter another seat number.")); } else { // Add the new barber, & allocate the entered seat no to him using (var transaction = entities.Database.BeginTransaction()) { tblbarber barber = new tblbarber { barber_name = barber_name, salon_id = salon_id, allocated_seat_no = allocated_seat_no, is_available = true }; entities.tblbarbers.Add(barber); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, barber.barber_id.ToString(), typeof(tblbarber).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Barber", ActionType.INSERT)); } } } else { return(Messages.GetInstance().HandleException("Failed to create barber! The entered seat number is not found in the salon. Please enter a valid seat number.")); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create barber.")); } }