public List <Receptmodels> Oldslumps(int user_id, DateTime date, DateTime dateto) { slump Slump = new slump(); postgres m = new postgres(); List <Receptmodels> mt = new List <Receptmodels>(); DataTable dt = new DataTable(); dt = m.SqlQuery("SELECT foodlist.date_now,foodlist.recept_id,recept.name FROM public.foodlist join public.recept on foodlist.recept_id = recept.id_recept Where foodlist.user_id = @id_user AND date_now BETWEEN @datefrom AND @dateto ORDER BY date_now DESC; ", postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id_user", user_id), new NpgsqlParameter("@dateto", dateto), new NpgsqlParameter("@datefrom", date) }); foreach (DataRow dr in dt.Rows) { Receptmodels r = new Receptmodels(); r.Id = (int)dr["recept_id"]; r.Name = dr["name"].ToString(); r.Date = (DateTime)dr["date_now"]; r.Weeknumbers = GetIso8601WeekOfYear(r.Date).ToString(); mt.Add(r); } return(mt); }
public List <Receptmodels> Slumplist(int user_id, DateTime date) { date = datefixer(date); List <Receptmodels> food_list = new List <Receptmodels>(); Receptmodels re = new Receptmodels(); var recpet = new Foodservices(); food_list = recpet.GetFood("SELECT * FROM recept WHERE id_recept IN (SELECT recept_id FROM users_has_recept WHERE user_id =@id_user)", user_id); int maxdays = 5; int maxnumber = food_list.Count; Random rnd = new Random(); List <Receptmodels> random_list = new List <Receptmodels>(); int i = 0; bool check = true; if (maxnumber > 7) { while (i < maxdays) { check = true; int number = rnd.Next(1, maxnumber); foreach (var item in random_list) { if (item.Id == food_list[number].Id) { check = false; } } if (check) { food_list[number].Date = date; random_list.Add(food_list[number]); i++; date = date.AddDays(1); } } } if (maxnumber <= 7) { re.Id = -10; re.Name = "Inte tillräckligt med maträtter för att slumpa fram en vecka!!!"; random_list.Add(re); } return(random_list); }