public int GetByiddate(int sobua, DateTime date, int id_nguoidung) { Theodoibuaan bien = db.Theodoibuaans.Where(x => DbFunctions.TruncateTime(x.Ngaythang) == date.Date && x.ID_Nguoidung == id_nguoidung && x.ID_sobuatrongngay == sobua).Take(1).SingleOrDefault(); if (bien != null) { return(bien.ID_theodoi); } else { return(0); } }
public bool Thembua(string username, string sobua, DateTime date, decimal pro, decimal fat, decimal carbs)//ajax json { var sob = Int32.Parse(sobua); var id = GetById(username).ID_Nguoidung; var totalcalo = pro * 4 + carbs * 4 + fat * 9; Theodoibuaan entity = new Theodoibuaan(); entity.ID_Nguoidung = id; entity.ID_sobuatrongngay = Int32.Parse(sobua); entity.Ngaythang = date; entity.Fat_ba = fat; entity.Protein_ba = pro; entity.Carb_ba = carbs; entity.Total_calos = totalcalo; int check = GetByiddate(sob, date, id); if (check != 0) { var buaaner = db.Theodoibuaans.Find(check); buaaner.ID_Nguoidung = id; buaaner.ID_sobuatrongngay = Int32.Parse(sobua); buaaner.Ngaythang = date; buaaner.Fat_ba = fat; buaaner.Protein_ba = pro; buaaner.Carb_ba = carbs; buaaner.Total_calos = totalcalo; db.SaveChanges(); return(true); } else { try { db.Theodoibuaans.Add(entity); db.SaveChanges(); return(true); } catch (Exception ex) { return(false); } } }