public Stream createFleaMarket(string username, string password, string name, string description, string logo, string category, string city, string street, string postcode, string latitude, string longitude) { // Check if parameters are valid, else return error Wrapper validateResult = Utility.fleamarketValidateRules(name, description, logo, category, city, street, postcode, latitude, longitude); if (validateResult.status) { // Check if user is valid, else return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Add info to market and add it to database ((fleamarkets)validateResult.result).users_id = ((users)authResult.result).id; ((fleamarkets)validateResult.result).created_on = DateTime.UtcNow; ((fleamarkets)validateResult.result).modified_on = DateTime.UtcNow; rest.fleamarkets.Add((fleamarkets)validateResult.result); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, validateResult.error, null); }
public Stream createOpening(string username, string password, string fleamarketId, string from, string to, string description) { // Parse id, else return error int parseId; if (int.TryParse(fleamarketId, out parseId)) { // Check parameters else return error Wrapper validateResult = Utility.openingValidateRules(from, to, description); if (validateResult.status) { // Check if user is valid, else return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Find market and its opening if user is authorised, else just return error fleamarkets fleamarket = rest.fleamarkets.Include("fleamarket_openings").Where(b => b.users_id == ((users)authResult.result).id && b.id == parseId).FirstOrDefault(); if (fleamarket != null) { // Insert opening into fleamarket ((fleamarket_openings)validateResult.result).fleamarkets_id = parseId; fleamarket.fleamarket_openings.Add((fleamarket_openings)validateResult.result); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 25, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, validateResult.error, null); } else return Response.Serialize(false, 24, null); }
/// <summary> /// Function registers a user with given username and password for the rest framework on the database /// </summary> /// <param name="username">Users username string</param> /// <param name="password">Users password string</param> /// <returns>Returns a class wrapper with user info, error code and whether it succeded</returns> public static Wrapper register(string username, string password) { // Check for errors with username and password, if any errors end function and return wrapper with error. int res = validateRules(username, password); if (res > 0) return new Wrapper(false, res, null); // Use database using (RestEntities rest = new RestEntities()) { // Does a user exist with given username else return wrapper error if (rest.users.Where(b => b.email == username).FirstOrDefault() == null) { // Create salt string salted = Auth.salt(16); // Create hash string hashed = Auth.hash(salted, password); // Create database user placeholder and insert created data users user = new users(); user.email = username; user.salt = salted; user.password_hash = hashed; user.created_on = DateTime.Now; user.modified_on = DateTime.Now; // Insert user into database rest.users.Add(user); // Save the database changes rest.SaveChanges(); // Return wrapper with function succeeded and user info return new Wrapper(true, 0, user); } else return new Wrapper(false, 5, null); } }
public Stream createReview(string username, string password, string fleamarketId, string stars, string text) { // Check for invalid parameters, if so return error. Wrapper resRules = Utility.reviewValidateRules(fleamarketId, stars, text); if (resRules.status) { // Check if user provided info is eligible else just return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Check database for review and if it corresponds to user else return error fleamarket_reviews review = rest.fleamarket_reviews.Where(b => b.fleamarkets_id == ((fleamarket_reviews)resRules.result).fleamarkets_id && b.users_id == ((users)authResult.result).id).FirstOrDefault(); if (review == null) { // Add new review and save changes ((fleamarket_reviews)resRules.result).users_id = ((users)authResult.result).id; rest.fleamarket_reviews.Add((fleamarket_reviews)resRules.result); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 11, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, resRules.error, null); }
public Stream updateReview(string username, string password, string fleamarketId, string stars, string text) { // Check and validate parameters, else return error Wrapper resRules = Utility.reviewValidateRules(fleamarketId, stars, text); if (resRules.status) { // Check if user exists, else return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Check if user got access to update review, and get review, else return error fleamarket_reviews review = rest.fleamarket_reviews.Where(b => b.fleamarkets_id == ((fleamarket_reviews)resRules.result).fleamarkets_id && b.users_id == ((users)authResult.result).id).FirstOrDefault(); if (review != null) { // Update review with new data and save changes review.stars = ((fleamarket_reviews)resRules.result).stars; review.text = ((fleamarket_reviews)resRules.result).text; rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 12, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, resRules.error, null); }
public Stream updateOpening(string username, string password, string openingId, string from, string to, string description) { // Parse id, else return error int parseId; if (int.TryParse(openingId, out parseId)) { // Check for invalid parameters, else return error Wrapper validateResult = Utility.openingValidateRules(from, to, description); if (validateResult.status) { // Check for valid user Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Find opening and check user is authorised, else return error fleamarket_openings opening = rest.fleamarket_openings.Include("fleamarkets").Where(b => b.id == parseId && b.fleamarkets.users_id == ((users)authResult.result).id).FirstOrDefault(); if (opening != null) { // Update opening opening.description = ((fleamarket_openings)validateResult.result).description; opening.from = ((fleamarket_openings)validateResult.result).from; opening.to = ((fleamarket_openings)validateResult.result).to; rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 27, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, validateResult.error, null); } else return Response.Serialize(false, 26, null); }
public Stream updateFleaMarket(string username, string password, string fleamarketId, string name, string description, string logo, string category, string city, string street, string postcode, string latitude, string longitude) { // Parse id, else return error int parsedIdInt; if (int.TryParse(fleamarketId, out parsedIdInt)) { // Check for valid parameters, else return error Wrapper validateResult = Utility.fleamarketValidateRules(name, description, logo, category, city, street, postcode, latitude, longitude); if (validateResult.status) { // Check for valid user Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Check for market and that user is authorised else return error fleamarkets fleamarket = rest.fleamarkets.Include("fleamarket_addresses").Where(b => b.id == parsedIdInt && b.users_id == ((users)authResult.result).id).FirstOrDefault(); if (fleamarketId != null) { // Add info to fleamarket address fleamarket.fleamarket_addresses.city = ((fleamarkets)validateResult.result).fleamarket_addresses.city; fleamarket.fleamarket_addresses.postcode = ((fleamarkets)validateResult.result).fleamarket_addresses.postcode; fleamarket.fleamarket_addresses.street = ((fleamarkets)validateResult.result).fleamarket_addresses.street; fleamarket.fleamarket_addresses.latitude = ((fleamarkets)validateResult.result).fleamarket_addresses.latitude; fleamarket.fleamarket_addresses.longitude = ((fleamarkets)validateResult.result).fleamarket_addresses.longitude; // Add fleamarket info and save it fleamarket.name = ((fleamarkets)validateResult.result).name; fleamarket.description = ((fleamarkets)validateResult.result).description; fleamarket.logo = ((fleamarkets)validateResult.result).logo; fleamarket.modified_on = DateTime.UtcNow; rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 19, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, validateResult.error, null); } else return Response.Serialize(false, 18, null); }
public Stream deleteReview(string username, string password, string fleamarketId) { // Parse fleamarket id else return error int parseId; if (int.TryParse(fleamarketId, out parseId)) { // Check if user is valid else return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Find review for fleamarket and check user has access else return error fleamarket_reviews review = rest.fleamarket_reviews.Where(b => b.fleamarkets_id == parseId && b.users_id == ((users)authResult.result).id).FirstOrDefault(); if (review != null) { // Remove found review and save changes rest.fleamarket_reviews.Remove(review); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 14, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, 13, null); }
public Stream deleteOpening(string username, string password, string openingId) { // Parse id, else return error int parseId; if (int.TryParse(openingId, out parseId)) { // Check for valid user Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Check for opening given and if user is authorised, else return error fleamarket_openings opening = rest.fleamarket_openings.Include("fleamarkets").Where(b => b.id == parseId && b.fleamarkets.users_id == ((users)authResult.result).id).FirstOrDefault(); if (opening != null) { // Delete opening rest.fleamarket_openings.Remove(opening); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 29, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, 28, null); }
public Stream deleteFleaMarket(string username, string password, string fleamarketId) { // Parse id, else return error int parseId; if (int.TryParse(fleamarketId, out parseId)) { // Check if user is valid, else return error Wrapper authResult = Auth.login(username, password); if (authResult.status) { // Use database using (RestEntities rest = new RestEntities()) { // Find fleamarket and check if user got access, else return error fleamarkets fleamarket = rest.fleamarkets.Where(b => b.users_id == ((users)authResult.result).id && b.id == parseId).FirstOrDefault(); if (fleamarket != null) { // Delete fleamarket rest.fleamarkets.Remove(fleamarket); rest.SaveChanges(); // Return success return Response.Serialize(true, 0, null); } else return Response.Serialize(false, 21, null); } } else return Response.Serialize(false, authResult.error, null); } else return Response.Serialize(false, 20, null); }