public HttpResponseMessage DELETE(int invoice_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()) { var entity = entities.tblinvoices.FirstOrDefault(e => e.invoice_id == invoice_id); if (entity == null) { return(Messages.GetInstance().HandleException("Delete failed! Invoice with id = ", invoice_id.ToString())); } else { using (var transaction = entities.Database.BeginTransaction()) { entities.tblinvoices.Remove(entity); Utilities.getInstance().UpdateChanges(entities, transaction, invoice_id.ToString(), typeof(tblinvoice).Name, ActionType.DELETE); return(Messages.GetInstance().HandleRequest("Invoice", ActionType.DELETE)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to delete invoice.")); } }
public HttpResponseMessage DELETE(int owner_id) { try { using (SalonDbEntities entities = new SalonDbEntities()) { var entity = entities.tblshop_owner.FirstOrDefault(e => e.owner_id == owner_id); if (entity == null) { return(Messages.GetInstance().HandleException("Delete failed! Shop owner with id = ", owner_id.ToString())); } else { using (var transaction = entities.Database.BeginTransaction()) { entities.tblshop_owner.Remove(entity); Utilities.getInstance().UpdateChanges(entities, transaction, owner_id.ToString(), typeof(tblshop_owner).Name, ActionType.DELETE); return(Messages.GetInstance().HandleRequest("Shop owner", ActionType.DELETE)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to delete shop owner.")); } }
public HttpResponseMessage Post([FromBody] JObject customer_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 name = customer_details["name"].ToString().Trim(); string mobile_no = customer_details["mobile_no"].ToString().Trim(); int salon_id = int.Parse(customer_details["salon_id"].ToString()); using (SalonDbEntities entities = new SalonDbEntities()) { // Check if the salon id exists if (salon_id != 0 && !entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } // Validate customer mobile if (!Utilities.getInstance().ValidateContactNumber(mobile_no)) { return(Messages.GetInstance().ValidateFields("Customer", ActionType.INSERT, isContactNumber: true)); } // Check if the customer mobile already exists in the particular salon. if (entities.tblcustomers.Any(e => e.mobile_no.ToString().Trim() == mobile_no && e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Failed to create customer! A customer with the same mobile no exists in salon id = " + salon_id)); } else { using (var transaction = entities.Database.BeginTransaction()) { tblcustomer obj = new tblcustomer { name = name, mobile_no = int.Parse(mobile_no), salon_id = salon_id, login_time = DateTime.Now }; entities.tblcustomers.Add(obj); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, obj.customer_id.ToString(), typeof(tblcustomer).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Customer", ActionType.INSERT)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create customer.")); } }
// Checks if the user exists public Boolean IsValidUser(string username) { using (SalonDbEntities entities = new SalonDbEntities()) { tblshop_owner selectedOwner = entities.tblshop_owner.FirstOrDefault(e => e.email == username); return((selectedOwner != null) ? true : false); } }
public HttpResponseMessage Put(int customer_id, [FromBody] JObject customer_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." }); using (SalonDbEntities entities = new SalonDbEntities()) { // Check if a customer with the specified Id exist var entity = entities.tblcustomers.FirstOrDefault(e => e.customer_id == customer_id); if (entity == null) { return(Messages.GetInstance().HandleException("Update failed! Customer with id = ", customer_id.ToString())); } string name = customer_details["name"].ToString().Trim(); string mobile_no = customer_details["mobile_no"].ToString().Trim(); // Validate customer mobile if (!Utilities.getInstance().ValidateContactNumber(mobile_no)) { return(Messages.GetInstance().ValidateFields("Customer", ActionType.UPDATE, isContactNumber: true)); } else { // Check if the customer mobile already exists in the particular salon. var customer = entities.tblcustomers.Where(p => p.salon_id == entity.salon_id && p.mobile_no.ToString().Trim() == mobile_no).Any(); if (entity.mobile_no.ToString().Trim() != mobile_no && customer) { return(Messages.GetInstance().HandleException("Update failed! A customer with the same mobile no already exists in salon id = " + entity.salon_id)); } else { using (var transaction = entities.Database.BeginTransaction()) { // Update necessary data fielsds entity.name = name; entity.mobile_no = int.Parse(mobile_no); Utilities.getInstance().UpdateChanges(entities, transaction, customer_id.ToString(), typeof(tblcustomer).Name, ActionType.UPDATE); return(Messages.GetInstance().HandleRequest("Customer", ActionType.UPDATE)); } } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to update customer details.")); } }
public HttpResponseMessage Put(int service_id, [FromBody] JObject service_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." }); using (SalonDbEntities entities = new SalonDbEntities()) { string service_name = service_details["service_name"].ToString().Trim(); Decimal price = Decimal.Parse(service_details["price"].ToString()); string duration = service_details["duration"].ToString().Trim(); // Check if a service with the specified id exist var entity = entities.tblservices.FirstOrDefault(e => e.service_id == service_id); if (entity == null) { return(Messages.GetInstance().HandleException("Update failed! Service with id = ", service_id.ToString())); } // If a service with the specified id exists else { // Check for duplicates - check if the service already exists in the particular salon bool selectedService = entities.tblservices.Any(e => e.service_name.ToUpper().Trim() == service_name.ToUpper().Trim() && e.salon_id == entity.salon_id); // If a another service already exists with the entered name & salon id if (entity.service_name.ToUpper().Trim() != service_name.ToUpper().Trim() && selectedService) { return(Messages.GetInstance().HandleRequest("Service", ActionType.UPDATE, true)); } // If a service doesn't exist then update the service using (var transaction = entities.Database.BeginTransaction()) { // Update necessary fields entity.service_name = service_name; entity.price = price; entity.duration = Convert.ToInt32(TimeSpan.Parse(duration).TotalSeconds); Utilities.getInstance().UpdateChanges(entities, transaction, service_id.ToString(), typeof(tblservice).Name, ActionType.UPDATE); return(Messages.GetInstance().HandleRequest("Service", ActionType.UPDATE)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to update service details.")); } }
// Updates changes in the database public void UpdateChanges(SalonDbEntities entities, DbContextTransaction transaction, string id, string table, ActionType actionType) { if (!actionType.Equals("INSERT")) { entities.SaveChanges(); } // Update log information Log.Update(id, table, actionType); transaction.Commit(); }
public HttpResponseMessage GetCurrentlyAvailableBarbers(int salon_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()) { // Check if the salon id exists if (!entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } // Get the currently available barbers in the particular salon var allAvailableBarbers = entities.tblbarbers.Where(x => x.salon_id == salon_id && x.is_available == true).ToList(); if (allAvailableBarbers != null && allAvailableBarbers.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Currently available barbers retrieved successfully!" }); foreach (var item in allAvailableBarbers) { responses.Add(new { item.barber_id, item.barber_name, item.salon_id, item.allocated_seat_no, item.is_available }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No barbers found!", All_barbers = allAvailableBarbers })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve currently available barbers.")); } }
// Checks if the entered password matches the exact password public bool IsValidPassword(string username, string password) { using (SalonDbEntities entities = new SalonDbEntities()) { // Get the encrypted password of the user from the db // Decrypt the db password string decryptedPwd = Utilities.getInstance().DecodeFrom64(GetUserData(username)); // Validate passwords - compare the two passwords return(password.Equals(decryptedPwd) ? true : false); } }
public HttpResponseMessage Get() { try { using (SalonDbEntities entities = new SalonDbEntities()) { List <tblshop_owner> allShopOwners = entities.tblshop_owner.ToList(); if (allShopOwners != null && allShopOwners.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Shop owners retrieved successfully!" }); List <Object> salon = new List <Object>(); foreach (var item in allShopOwners) { var salons = entities.tblsalons.Where(x => x.owner_id == item.owner_id).Select(x => x.salon_id).ToArray(); foreach (int salonId in salons) { salon.Add(new { salonId, salon_name = entities.tblsalons.Where(x => x.salon_id == salonId).Select(x => x.salon_name).First(), }); } responses.Add(new { item.owner_id, item.name, item.contact_no, item.email, item.password, item.pin, owning_salons = salon }); salon = new List <Object>(); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No shop owners found!", All_shop_owners = allShopOwners })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve shop owners.")); } }
public HttpResponseMessage GetServicesOfBarber(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()) { // If the barber id doesn't exists if (entities.tblbarbers.FirstOrDefault(e => e.barber_id == barber_id) == null) { return(Messages.GetInstance().HandleException("Retrieve failed! Barber with id = ", barber_id.ToString())); } // If the barber doesn't have any services if (entities.tblbarber_service.FirstOrDefault(e => e.barber_id == barber_id) == null) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No services found for barber id = " + barber_id })); } else { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "All services performed by barber id = " + barber_id + " retrieved successfully!" }); var barber_service = entities.tblbarber_service.Where(p => p.barber_id.Equals(barber_id)).Select(x => x.service_id).ToList(); foreach (int item in barber_service) { responses.Add(new { service_id = item, service_name = entities.tblservices.Where(x => x.service_id == item).Select(x => x.service_name).First() }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve services performed by barber.")); } }
public HttpResponseMessage Get(int salon_id = 0) { 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()) { // Check if the salon id exists if (salon_id != 0 && !entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } // If the salon id isn't specified, then retrieve all services, else get only the services available in the specified salon List <tblservice> allAvailableServices = (salon_id == 0) ? entities.tblservices.ToList() : entities.tblservices.Where(s => s.salon_id == salon_id).ToList(); if (allAvailableServices != null && allAvailableServices.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Services retrieved successfully!" }); foreach (var item in allAvailableServices) { responses.Add(new { item.service_id, item.service_name, item.salon_id, item.price, item.duration }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No services found!", All_services = allAvailableServices })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve services.")); } }
/// <summary> /// Save data modification details to the update table /// </summary> /// <param name="id">Represents references</param> /// <param name="table">Represents the reference table</param> /// <param name="actionType">Reference the table modification type</param> public static void Update(string id, string table, ActionType actionType) { SalonDbEntities db = new SalonDbEntities(); tbllog update = new tbllog { ref_table = table, ref_id = id, updated_date_time = DateTime.Now, action_type = System.Enum.GetName(typeof(ActionType), actionType) }; db.tbllogs.Add(update); db.SaveChanges(); }
public HttpResponseMessage Post([FromBody] JObject service_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 service_name = service_details["service_name"].ToString().Trim(); int salon_id = int.Parse(service_details["salon_id"].ToString()); Decimal price = Decimal.Parse(service_details["price"].ToString()); string duration = service_details["duration"].ToString().Trim(); using (SalonDbEntities entities = new SalonDbEntities()) { // Validate service - check if the service already exists in that particular salon bool selectedService = entities.tblservices.Any(e => e.service_name.ToUpper().Trim() == service_name.ToUpper().Trim() && e.salon_id == salon_id); // If a service already exists if (selectedService) { return(Messages.GetInstance().HandleRequest("Service", ActionType.INSERT, true)); } else { using (var transaction = entities.Database.BeginTransaction()) { tblservice obj = new tblservice { service_name = service_name, salon_id = salon_id, price = price, duration = Convert.ToInt32(TimeSpan.Parse(duration).TotalSeconds) }; entities.tblservices.Add(obj); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, obj.service_id.ToString(), typeof(tblservice).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Service", ActionType.INSERT)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create service.")); } }
// Calculates the total price, for the requested services public decimal CalculateTotal(int salon_id, int[] requested_services) { decimal total = 0; using (SalonDbEntities entities = new SalonDbEntities()) { foreach (int service in requested_services) { // Get the price of the service total += entities.tblservices.Where(e => e.salon_id == salon_id && e.service_id == service).Select(x => x.price).First(); } } return(total); }
public HttpResponseMessage Get(int owner_id) { try { using (SalonDbEntities entities = new SalonDbEntities()) { tblshop_owner selectedOwner = entities.tblshop_owner.FirstOrDefault(e => e.owner_id == owner_id); if (selectedOwner != null) { List <Object> salon = new List <Object>(); var salons = entities.tblsalons.Where(x => x.owner_id == selectedOwner.owner_id).Select(x => x.salon_id).ToArray(); foreach (int salonId in salons) { salon.Add(new { salonId, salon_name = entities.tblsalons.Where(x => x.salon_id == salonId).Select(x => x.salon_name).First(), }); } return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Shop owner retrieved successfully!", Shop_owner_details = new { selectedOwner.owner_id, selectedOwner.name, selectedOwner.contact_no, selectedOwner.email, selectedOwner.password, selectedOwner.pin, owning_salons = salon } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Shop owner with id = ", owner_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve shop owner details.")); } }
public HttpResponseMessage Get(int salon_id = 0) { 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()) { // Check if the salon id exists if (salon_id != 0 && !entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } List <tblcustomer> allCustomers = (salon_id == 0) ? entities.tblcustomers.ToList() : entities.tblcustomers.Where(x => x.salon_id == salon_id).ToList(); if (allCustomers != null && allCustomers.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Customers retrieved successfully!" }); foreach (var item in allCustomers) { responses.Add(new { item.customer_id, item.salon_id, item.name, item.mobile_no, item.login_time }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No customers found!", All_customers = allCustomers })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve customers.")); } }
public HttpResponseMessage Get() { 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()) { List <tblsalon> allRegisteredSalons = entities.tblsalons.ToList(); if (allRegisteredSalons != null && allRegisteredSalons.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Salons retrieved successfully!" }); foreach (var item in allRegisteredSalons) { responses.Add(new { item.salon_id, item.salon_name, item.owner_id, owner_name = entities.tblshop_owner.Where(x => x.owner_id == item.owner_id).Select(x => x.name).First(), item.salon_location, item.contact_no, item.email, item.seating_capacity, item.opening_time, item.closing_time }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No salons found!", All_salons = allRegisteredSalons })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve salons.")); } }
// Get password of the user from the db public string GetUserData(string username, bool isPwd = true, bool isUserId = false) { using (SalonDbEntities entities = new SalonDbEntities()) { if (isPwd) { tblshop_owner selectedOwner = entities.tblshop_owner.FirstOrDefault(e => e.email == username); return((selectedOwner != null) ? selectedOwner.password : null); } else if (isUserId) { tblshop_owner selectedOwner = entities.tblshop_owner.FirstOrDefault(e => e.email == username); return((selectedOwner != null) ? selectedOwner.owner_id.ToString() : null); } return(null); } }
public HttpResponseMessage GenerateInvoiceAutomatically(int salon_id, int appointment_id, decimal discount = 0) { 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()) { // Check if an invoice is already existing for the particular appointment if (entities.tblinvoices.Any(e => e.appointment_id == appointment_id)) { return(Messages.GetInstance().HandleException("Failed to create invoice! An invoice already exists for appointment id = " + appointment_id)); } using (var transaction = entities.Database.BeginTransaction()) { int[] requested_services = entities.tblservice_booked.Where(x => x.appointment_id == appointment_id).Select(x => x.service_id).ToArray(); decimal totalPrice = CalculateTotal(salon_id, requested_services); tblinvoice invoice = new tblinvoice { salon_id = salon_id, appointment_id = appointment_id, total_price = totalPrice, discount = discount, final_price = CalculateFinalTotal(totalPrice, discount), }; entities.tblinvoices.Add(invoice); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, invoice.invoice_id.ToString(), typeof(tblinvoice).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Invoice", ActionType.INSERT)); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create invoice.")); } }
public HttpResponseMessage Get(int salon_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()) { tblsalon selectedSalon = entities.tblsalons.FirstOrDefault(e => e.salon_id == salon_id); if (selectedSalon != null) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Salon retrieved successfully!", Salon_details = new { selectedSalon.salon_id, selectedSalon.salon_name, selectedSalon.owner_id, owner_name = entities.tblshop_owner.Where(x => x.owner_id == selectedSalon.owner_id).Select(x => x.name).First(), selectedSalon.salon_location, selectedSalon.contact_no, selectedSalon.email, selectedSalon.seating_capacity, selectedSalon.opening_time, selectedSalon.closing_time } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve salon details.")); } }
public HttpResponseMessage Post([FromBody] JObject barber_service_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." }); using (SalonDbEntities entities = new SalonDbEntities()) { int barber_id = int.Parse(barber_service_details["barber_id"].ToString()); int service_id = int.Parse(barber_service_details["service_id"].ToString()); // Validate barber - check if the barber service already exists if (entities.tblbarber_service.Any(e => e.barber_id == barber_id && e.service_id == service_id)) { return(Messages.GetInstance().HandleRequest("Barber Service", ActionType.INSERT, true)); } else { using (var transaction = entities.Database.BeginTransaction()) { tblbarber_service obj = new tblbarber_service { barber_id = barber_id, service_id = service_id }; entities.tblbarber_service.Add(obj); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, obj.barber_service_id.ToString(), typeof(tblbarber).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Barber Service", ActionType.INSERT)); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create barber service.")); } }
public HttpResponseMessage GetSelectedBarberService(int barber_service_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_service selectedBarber = entities.tblbarber_service.FirstOrDefault(e => e.barber_service_id == barber_service_id); if (selectedBarber != null) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Barber service details retrieved successfully!", Barber_service_details = new { selectedBarber.barber_service_id, selectedBarber.barber_id, barber_name = entities.tblbarbers.Where(x => x.barber_id == selectedBarber.barber_id).Select(x => x.barber_name).First(), selectedBarber.service_id, service_name = entities.tblservices.Where(x => x.service_id == selectedBarber.service_id).Select(x => x.service_name).First() } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Barber service with id = ", barber_service_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve barber service details.")); } }
public HttpResponseMessage GetSelectedCustomer(int customer_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()) { tblcustomer selectedCustomer = entities.tblcustomers.FirstOrDefault(e => e.customer_id == customer_id); if (selectedCustomer != null) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Customer retrieved successfully!", Customer_details = new { selectedCustomer.customer_id, selectedCustomer.salon_id, selectedCustomer.name, selectedCustomer.mobile_no, selectedCustomer.login_time } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Customer with id = ", customer_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve customer details.")); } }
public HttpResponseMessage GetAllInvoices(int salon_id = 0) { 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()) { // Check if the salon id exists if (salon_id != 0 && !entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } List <tblinvoice> allInvoices = (salon_id == 0) ? entities.tblinvoices.ToList() : entities.tblinvoices.Where(x => x.salon_id == salon_id).ToList(); if (allInvoices != null && allInvoices.Count != 0) { List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Invoices retrieved successfully!" }); List <Object> services = new List <Object>(); foreach (var item in allInvoices) { int customerId = entities.tblappointments.Where(x => x.appointment_id == item.appointment_id).Select(x => x.customer_id).FirstOrDefault(); var requestedServices = entities.tblservice_booked.Where(x => x.appointment_id == item.appointment_id).Select(x => x.service_id).ToArray(); foreach (int serviceId in requestedServices) { services.Add(new { serviceId, service_name = entities.tblservices.Where(x => x.service_id == serviceId).Select(x => x.service_name).First(), price = entities.tblservices.Where(x => x.service_id == serviceId).Select(x => x.price).First() }); } responses.Add(new { item.invoice_id, date_created = entities.tblappointments.Where(x => x.appointment_id == item.appointment_id).Select(x => x.due_date).First(), item.salon_id, salon_name = entities.tblsalons.Where(x => x.salon_id == item.salon_id).Select(x => x.salon_name).First(), customer_id = customerId, customer_name = entities.tblcustomers.Where(x => x.customer_id == customerId).Select(x => x.name).FirstOrDefault(), item.appointment_id, offered_services = services, item.total_price, item.discount, item.final_price }); services = new List <Object>(); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No invoices found!", All_invoices = allInvoices })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve invoices.")); } }
public HttpResponseMessage Put(int invoice_id, [FromBody] JObject invoice_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." }); int salon_id = int.Parse(invoice_details["salon_id"].ToString()); int appointment_id = int.Parse(invoice_details["appointment_id"].ToString()); decimal discount = decimal.Parse(invoice_details["discount"].ToString()); int[] requested_services = new int[invoice_details["requested_services"].Count()]; int count = 0; foreach (var service in invoice_details["requested_services"]) { requested_services[count] = int.Parse(invoice_details["requested_services"][count].ToString()); count++; } using (SalonDbEntities entities = new SalonDbEntities()) { using (var transaction = entities.Database.BeginTransaction()) { // Check if an invoice with the specified id exist var entity = entities.tblinvoices.FirstOrDefault(e => e.invoice_id == invoice_id); if (entity == null) { return(Messages.GetInstance().HandleException("Update failed! Invoice with id = ", invoice_id.ToString())); } //Check if an invoice already exists for the particular appointment var invoice = entities.tblinvoices.Where(p => p.appointment_id == appointment_id).Any(); if (entity.appointment_id != appointment_id && invoice) { return(Messages.GetInstance().HandleException("Update failed! An invoice already exists for appointment id = " + appointment_id)); } // Check if the requested services exist in the given salon foreach (int service in requested_services) { if (!entities.tblservices.Any(x => x.salon_id == salon_id && x.service_id == service)) { return(Messages.GetInstance().HandleException("Update failed! Requested service doesn't exist in the given salon.")); } } // Check if the services have been requested in the given appointment foreach (int service in requested_services) { if (!entities.tblservice_booked.Any(x => x.appointment_id == appointment_id && x.service_id == service)) { return(Messages.GetInstance().HandleException("Update failed! Service id = " + service + " has not been requested, in the given appointment.")); } } decimal totalPrice = CalculateTotal(salon_id, requested_services); // Update necessary fielsds entity.appointment_id = appointment_id; entity.total_price = totalPrice; entity.discount = discount; entity.final_price = CalculateFinalTotal(totalPrice, discount); Utilities.getInstance().UpdateChanges(entities, transaction, entity.invoice_id.ToString(), typeof(tblinvoice).Name, ActionType.UPDATE); return(Messages.GetInstance().HandleRequest("Invoice", ActionType.UPDATE)); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to update invoice details.")); } }
public HttpResponseMessage Post([FromBody] JObject invoice_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." }); using (SalonDbEntities entities = new SalonDbEntities()) { int salon_id = int.Parse(invoice_details["salon_id"].ToString()); int appointment_id = int.Parse(invoice_details["appointment_id"].ToString()); decimal discount = decimal.Parse(invoice_details["discount"].ToString()); int[] requested_services = new int[invoice_details["requested_services"].Count()]; int count = 0; foreach (var service in invoice_details["requested_services"]) { requested_services[count] = int.Parse(invoice_details["requested_services"][count].ToString()); count++; } // Check if an invoice is already existing for the particular appointment if (entities.tblinvoices.Any(e => e.appointment_id == appointment_id)) { return(Messages.GetInstance().HandleException("Failed to create invoice! An invoice already exists for appointment id = " + appointment_id)); } // Check if the requested services exist in the given salon foreach (int service in requested_services) { if (!entities.tblservices.Any(x => x.salon_id == salon_id && x.service_id == service)) { return(Messages.GetInstance().HandleException("Failed to create invoice! Requested service doesn't exist in the given salon.")); } } // Check if the services have been requested in the given appointment foreach (int service in requested_services) { if (!entities.tblservice_booked.Any(x => x.appointment_id == appointment_id && x.service_id == service)) { return(Messages.GetInstance().HandleException("Failed to create invoice! Service id = " + service + " has not been requested, in the given appointment.")); } } using (var transaction = entities.Database.BeginTransaction()) { decimal totalPrice = CalculateTotal(salon_id, requested_services); tblinvoice invoice = new tblinvoice { salon_id = salon_id, appointment_id = appointment_id, total_price = CalculateTotal(salon_id, requested_services), discount = discount, final_price = CalculateFinalTotal(totalPrice, discount) }; entities.tblinvoices.Add(invoice); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, invoice.invoice_id.ToString(), typeof(tblinvoice).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Invoice", ActionType.INSERT)); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create invoice.")); } }
public HttpResponseMessage Get(int invoice_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()) { tblinvoice selectedInvoice = entities.tblinvoices.FirstOrDefault(e => e.invoice_id == invoice_id); if (selectedInvoice != null) { List <Object> services = new List <Object>(); int customerId = entities.tblappointments.Where(x => x.appointment_id == selectedInvoice.appointment_id).Select(x => x.customer_id).FirstOrDefault(); var requestedServices = entities.tblservice_booked.Where(x => x.appointment_id == selectedInvoice.appointment_id).Select(x => x.service_id).ToArray(); foreach (int serviceId in requestedServices) { services.Add(new { serviceId, service_name = entities.tblservices.Where(x => x.service_id == serviceId).Select(x => x.service_name).First(), price = entities.tblservices.Where(x => x.service_id == serviceId).Select(x => x.price).First() }); } return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "Invoice retrieved successfully!", Invoice_details = new { selectedInvoice.invoice_id, date_created = entities.tblappointments.Where(x => x.appointment_id == selectedInvoice.appointment_id).Select(x => x.due_date).First(), selectedInvoice.salon_id, salon_name = entities.tblsalons.Where(x => x.salon_id == selectedInvoice.salon_id).Select(x => x.salon_name).First(), customer_id = customerId, customer_name = entities.tblcustomers.Where(x => x.customer_id == customerId).Select(x => x.name).FirstOrDefault(), selectedInvoice.appointment_id, offered_services = services, selectedInvoice.total_price, selectedInvoice.discount, selectedInvoice.final_price } })); } else { return(Messages.GetInstance().HandleException("Retrieve failed! Invoice with id = ", invoice_id.ToString())); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve invoice details.")); } }
public HttpResponseMessage Get(int salon_id = 0) { 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()) { // Check if the salon id exists if (salon_id != 0 && !entities.tblsalons.Any(e => e.salon_id == salon_id)) { return(Messages.GetInstance().HandleException("Retrieve failed! Salon with id = ", salon_id.ToString())); } List <tblbarber_service> allServices = entities.tblbarber_service.ToList(); if (allServices != null && allServices.Count != 0) { if (salon_id != 0) { foreach (var service in allServices.ToList()) { if (!entities.tblbarbers.Any(x => x.barber_id == service.barber_id && x.salon_id == salon_id)) { allServices.Remove(service); } } } if (allServices.Count == 0) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No services found!" })); } List <Object> responses = new List <Object>(); responses.Add(new { Success = true, Message = "Services performed by all barbers retrieved successfully!" }); foreach (var item in allServices) { responses.Add(new { item.barber_service_id, item.barber_id, barber_name = entities.tblbarbers.Where(x => x.barber_id == item.barber_id).Select(x => x.barber_name).First(), item.service_id, service_name = entities.tblservices.Where(x => x.service_id == item.service_id).Select(x => x.service_name).First() }); } return(Request.CreateResponse(HttpStatusCode.OK, responses)); } else { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "No services performed by any barber!", All_barber_services = allServices })); } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve services performed by all barbers.")); } }
public HttpResponseMessage Get(int salon_id, int barber_id) { try { using (SalonDbEntities entities = new SalonDbEntities()) { // Check if the barber exists, in the given salon if (!entities.tblbarbers.Any(e => e.salon_id == salon_id && e.barber_id == barber_id)) { return(Request.CreateResponse(HttpStatusCode.NotFound, new { Success = false, Message = "Retrieve failed! Barber doesn't exist in the given salon." })); } DateTime currentDate = DateTime.Now.Date; List <Object> response = new List <Object>(); // Check if the barber has any appointment(s) for today List <tblappointment> appointmentsForToday = entities.tblappointments.Where(x => x.due_date.Equals(currentDate) && x.salon_id == salon_id && x.barber_Id == barber_id).ToList(); if (appointmentsForToday.Count == 0) { response.Add(new { Success = true, Message = "No appointments scheduled for today.", Current_appointment_no = 0 }); return(Request.CreateResponse(HttpStatusCode.OK, response)); } // Check if the barber has started his appointment(s) for today var allAppointmentsMade = entities.tblcurrent_appointments.Where(x => x.current_date.Equals(currentDate) && x.salon_id == salon_id && x.barber_id == barber_id).FirstOrDefault(); int nextAppointment = 0; if (!entities.tblappointments.Any(x => x.due_date.Equals(currentDate) && x.salon_id == salon_id && x.barber_Id == barber_id && x.status == AppointmentStatus.TO_DO.ToString())) { return(Request.CreateResponse(HttpStatusCode.OK, new { Success = true, Message = "All appointments made to barber id = " + barber_id + " in salon id = " + salon_id + ", which are scheduled for today, have been completed!", Current_appointment_no = 0 })); } if (allAppointmentsMade != null) { nextAppointment = allAppointmentsMade.last_appointment_no + 1; } // If the barber has any appointment(s) scheduled for today, but if they have not started yet else { nextAppointment = 1; } response.Add(new { Success = true, Message = "Current appointment no for barber id = " + barber_id + " in salon id = " + salon_id + " retrieved successfully!", Current_appointment_no = nextAppointment }); using (var transaction = entities.Database.BeginTransaction()) { // Update the status of the appointment tblappointment entity = entities.tblappointments.Where(x => x.due_date.Equals(currentDate) && x.salon_id == salon_id && x.barber_Id == barber_id && x.appointment_no_for_day == nextAppointment).FirstOrDefault(); if (entity != null) { entity.status = AppointmentStatus.IN_PROGRESS.ToString(); entities.SaveChanges(); Log.Update(entity.appointment_id.ToString(), typeof(tblappointment).Name, ActionType.UPDATE); } // Update the availability of barber var barber = entities.tblbarbers.Where(x => x.salon_id == salon_id && x.barber_id == barber_id).FirstOrDefault(); if (barber != null) { barber.is_available = false; entities.SaveChanges(); Log.Update(barber.barber_id.ToString(), typeof(tblbarber).Name, ActionType.UPDATE); } transaction.Commit(); } return(Request.CreateResponse(HttpStatusCode.OK, response)); } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to retrieve current appointment number.")); } }