//העלאת תמונת חיית מחמד public static Pets SavePetImage(string petID, string imgPath) { Pets p = null; comm.CommandText = $"UPDATE PetDetails" + $" SET Image='{imgPath}' WHERE PetID='{int.Parse(petID)}'"; comm.Connection.Open(); int res = comm.ExecuteNonQuery(); if (res == 1) { p = new Pets() { PetID = int.Parse(petID), Image = imgPath }; } comm.Connection.Close(); return(p); }
//הצגת כל החיות מחמד של המשתמש public static List <Pets> ShowMyPets(int user_id) { List <Pets> pets = new List <Pets>(); Pets p = null; comm.CommandText = $"SELECT * FROM PetDetails WHERE UserCode='{user_id}'"; comm.Connection.Open(); SqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { p = new Pets() { PetID = int.Parse(reader["PetID"].ToString()), Name = reader["Name"].ToString(), }; pets.Add(p); } comm.Connection.Close(); return(pets); }
//הרשמה של משתמש אל האפליקציה public static Pets PetRegistration(string name, int age, int raceCode, bool isDog, int userCode, char gender, string vaccines, string img = "") { Pets p = null; SqlDataReader reader = null; //SqlDataReader reader2 = null; try { //if (comm.Connection.State != ConnectionState.Closed) { // comm.Connection.Close(); //} comm.CommandText = $"INSERT INTO PetDetails(Name,Age,RaceCode,IsDog,UserCode,Gender,Vaccines,Image)VALUES" + $"('{name}',{age},{raceCode},'{isDog}',{userCode},'{gender}','{vaccines}','{img}')"; comm.Connection.Open(); int res = comm.ExecuteNonQuery(); if (res == 1) { comm.CommandText = "SELECT max(PetID) as maxID FROM PetDetails"; reader = comm.ExecuteReader(); if (reader.Read()) { p = new Pets() { PetID = (int)reader["maxID"], Name = name, Age = age, RaceCode = raceCode, IsDog = isDog, UserCode = userCode, Gender = gender, Vaccines = vaccines, Gallery = new List <string>() }; string path = HttpContext.Current.Server.MapPath("~/ImageStorage/Pets/" + p.PetID + @"/"); string[] files = (Directory.Exists(path)) ? Directory.GetFiles(path) : new string[0]; foreach (string file in files) { p.Gallery.Add(Path.GetFileName(file)); } return(p); } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (!reader.IsClosed) { reader.Close(); } if (comm.Connection.State != ConnectionState.Closed) { comm.Connection.Close(); } } return(p); }
//שליפת טבלת חיות המחמד - שי אברהם public static List <Pets> GetPetsInfo(int isDog, int regionId, bool sortByAge, bool sortByGender) { List <Pets> pets = new List <Pets>(); Pets p = null; string sql = "Exec GetPetsInfo "; sql += (isDog != -1) ? $"'where IsDog = {isDog} " : ""; if (regionId != 0) { sql += (isDog != -1) ? $"and RegionID = {regionId}', " : $"'where RegionID = {regionId}', "; } else { sql += (isDog != -1) ? $"', " : $"NULL, "; } sql += (sortByAge) ? "'order by Age" : ""; if (sortByGender) { sql += (!sortByAge) ? "'order by Gender'" : ", Gender'"; } else { sql += (sortByAge) ? "'" : "NULL"; } comm.CommandText = sql; comm.Connection.Close(); comm.Connection.Open(); SqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { p = new Pets() { PetID = int.Parse(reader["PetID"].ToString()), Name = reader["Name"].ToString(), Age = int.Parse(reader["Age"].ToString()), RaceCode = int.Parse(reader["RaceCode"].ToString()), IsDog = bool.Parse(reader["IsDog"].ToString()), UserCode = int.Parse(reader["UserCode"].ToString()), Gender = char.Parse(reader["Gender"].ToString()), Vaccines = reader["Vaccines"].ToString(), RegionID = int.Parse(reader["RegionID"].ToString()), Gallery = new List <string>() }; pets.Add(p); } for (int i = 0; i < pets.Count; i++) { string path = HttpContext.Current.Server.MapPath("~/ImageStorage/Pets/" + pets[i].PetID.ToString()); string[] files = (Directory.Exists(path)) ? Directory.GetFiles(path) : new string[0]; foreach (string file in files) { pets[i].Gallery.Add(Path.GetFileName(file)); } } comm.Connection.Close(); return(pets); }