// 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 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.")); } }
// 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 Post([FromBody] JObject owner_details) { try { string name = owner_details["name"].ToString().Trim(); string contact_no = owner_details["contact_no"].ToString().Trim(); string pin = owner_details["pin"].ToString().Trim(); string password = owner_details["password"].ToString().Trim(); string email = null; if (owner_details["email"] != null) { email = owner_details["email"].ToString().Trim(); } using (SalonDbEntities entities = new SalonDbEntities()) { // Validate the contact no if (!Utilities.getInstance().ValidateContactNumber(contact_no)) { return(Messages.GetInstance().ValidateFields("Shop owner", ActionType.INSERT, isContactNumber: true)); } // Validates the email if (email != null && !Utilities.getInstance().ValidateEmail(email)) { return(Messages.GetInstance().ValidateFields("Shop owner", ActionType.INSERT, isEmail: true)); } // Validates the pin if (pin.Count() != 5 || !Regex.IsMatch(pin, @"^\d{5}$")) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new { Success = false, Message = "Failed to create shop owner! Received invalid pin. Hint: Pin should contain only 5 digits." })); } // Validates the password if (!Utilities.getInstance().ValidatePassword(password)) { return(Messages.GetInstance().ValidateFields("Shop owner", ActionType.INSERT, isPassword: true)); } // Check if another shop owner already exists with the same contact no or email or username if (entities.tblshop_owner.Any(e => e.contact_no.ToString() == contact_no)) { return(Messages.GetInstance().HandleException("Failed to create shop owner! Contact number already exists.")); } // Checks if the user pin alreeady exists var userPins = entities.tblshop_owner.Select(x => x.pin).ToList(); foreach (string o in userPins) { if (Utilities.getInstance().DecodeFrom64(o) == pin) { return(Messages.GetInstance().HandleException("Failed to create shop owner! Pin already exists.")); } } if (email != null && entities.tblshop_owner.Any(e => e.email != null && e.email == email)) { return(Messages.GetInstance().HandleException("Failed to create shop owner! Email already exists.")); } else { // Add the new shop owner using (var transaction = entities.Database.BeginTransaction()) { tblshop_owner owner = new tblshop_owner { name = name, contact_no = int.Parse(contact_no), email = email, password = Utilities.getInstance().CalculateHash(password), pin = Utilities.getInstance().CalculateHash(pin) }; entities.tblshop_owner.Add(owner); entities.SaveChanges(); Utilities.getInstance().UpdateChanges(entities, transaction, owner.owner_id.ToString(), typeof(tblshop_owner).Name, ActionType.INSERT); return(Messages.GetInstance().HandleRequest("Shop owner", ActionType.INSERT)); //return Request.CreateResponse(HttpStatusCode.Created, new { Login = true, Pin = pin }); } } } } catch (Exception) { return(Messages.GetInstance().HandleException("An error occured! Failed to create shop owner.")); } }