Пример #1
0
        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."));
            }
        }
Пример #2
0
        public HttpResponseMessage Post([FromBody] JObject salon_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    owner_id     = int.Parse(salon_details["owner_id"].ToString());
                string name         = salon_details["name"].ToString();
                string location     = salon_details["location"].ToString();
                string contact_no   = salon_details["contact_no"].ToString().Trim();
                string email        = salon_details["email"].ToString();
                int    no_of_seats  = int.Parse(salon_details["no_of_seats"].ToString());
                string opening_time = salon_details["opening_time"].ToString();
                string closing_time = salon_details["closing_time"].ToString();

                using (SalonDbEntities entities = new SalonDbEntities())
                {
                    // Validate salon - check if the salon already exists
                    bool selectedSalon = entities.tblsalons.Any(e => e.contact_no.ToString().Trim() == contact_no || e.email.ToUpper().Trim() == email.ToUpper().Trim());

                    // If a salon already exists
                    if (selectedSalon)
                    {
                        return(Messages.GetInstance().HandleRequest("Salon", ActionType.INSERT, true));
                    }
                    else
                    {
                        // Validates the contact no
                        if (!Utilities.getInstance().ValidateContactNumber(contact_no))
                        {
                            return(Messages.GetInstance().ValidateFields("Salon", ActionType.INSERT, isContactNumber: true));
                        }

                        // Validates the email
                        if (email != null && !Utilities.getInstance().ValidateEmail(email))
                        {
                            return(Messages.GetInstance().ValidateFields("Salon", ActionType.INSERT, isEmail: true));
                        }

                        // Validates the no of seats
                        if (no_of_seats <= 0)
                        {
                            return(Messages.GetInstance().HandleException("Failed to create salon! No of seats should be > 0."));
                        }

                        using (var transaction = entities.Database.BeginTransaction())
                        {
                            tblsalon obj = new tblsalon
                            {
                                owner_id         = owner_id,
                                salon_name       = name.Trim(),
                                salon_location   = location.Trim(),
                                contact_no       = int.Parse(contact_no),
                                email            = email.Trim(),
                                seating_capacity = no_of_seats,
                                opening_time     = DateTime.Parse(opening_time, System.Globalization.CultureInfo.CurrentCulture).TimeOfDay,
                                closing_time     = DateTime.Parse(closing_time, System.Globalization.CultureInfo.CurrentCulture).TimeOfDay
                            };
                            entities.tblsalons.Add(obj);
                            entities.SaveChanges();

                            Utilities.getInstance().UpdateChanges(entities, transaction, obj.salon_id.ToString(), typeof(tblsalon).Name, ActionType.INSERT);

                            return(Messages.GetInstance().HandleRequest("Salon", ActionType.INSERT));
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(Messages.GetInstance().HandleException("An error occured! Failed to create salon."));
            }
        }