public Config GetConfig(string Key) { using (var context = new SDContext()) { return(context.Configurations.Find(Key)); } }
/// <summary> /// Gets a log by id. /// </summary> /// <param name="id"></param> /// <returns></returns> public LogModel GetById(Guid id) { if (id != null && id != Guid.Empty) { using (SDContext sdContext = new SDContext()) { Log log = sdContext.Log.Get(id); if (log == null) { return(null); } LogModel model = new LogModel() { Id = log.Id, Date = log.Date, Exception = log.Exception, Extra = log.Extra, LogType = Logger.GetLogTypeString(log.LogTypeId), LogTypeSub = Logger.GetLogTypeSubString(log.LogTypeSubId), LogTypeId = log.LogTypeId, LogTypeSubId = log.LogTypeSubId, Message = log.Message, Url = log.Url, UserId = log.UserId }; return(model); } } else { return(null); } }
/// <summary> /// Verifying the encrypted user. /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> private static bool VerifyEncryptedUser(string email, string password, string userId, string phoneNumber) { if (!String.IsNullOrEmpty(email) && !String.IsNullOrEmpty(password)) { using (SDContext sdContext = new SDContext()) { EncryptedUserData encUser = sdContext.EncryptedUserData.GetFirstOrDefault(x => x.UserId == userId); if (encUser == null) { return(false); } if (!HashData.VerifyHash(email, SHA256, encUser.Email) && !HashData.VerifyHash(password, SHA256, encUser.Password) && !HashData.VerifyHash(phoneNumber, SHA256, encUser.PhoneNumber)) { return(false); } else { return(true); } } } else { return(false); } }
/// <summary> /// Deletes the teacher entity. /// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(Guid id) { try { if (id == null || id == Guid.Empty) { return(false); } using (SDContext sdContext = new SDContext()) { Teacher teacher = sdContext.Teacher.Get(id); if (teacher == null) { return(false); } sdContext.Teacher.Delete(id); sdContext.Save(); new UserBuilder().DeleteApplicationUser(teacher.UserId, teacher.Email); } return(true); } catch (Exception ex) { Logger.Write(ex); } return(false); }
/// <summary> /// Inserts a new user to the database. /// </summary> /// <param name="model"></param> /// <param name="userId"></param> /// <returns></returns> public bool Insert(RegisterViewModel model, string userId) { try { if (model != null && !String.IsNullOrEmpty(model.Email) && !String.IsNullOrEmpty(model.Password) && !String.IsNullOrEmpty(model.Firstname) && !String.IsNullOrEmpty(model.Lastname)) { using (SDContext sdContext = new SDContext()) { sdContext.Teacher.Insert(new Teacher() { Id = Guid.NewGuid(), Email = model.Email, Firstname = model.Firstname, Lastname = model.Lastname, LastLogin = DateTime.Now, UserId = userId }); sdContext.Save(); } return(true); } else { return(false); } } catch (Exception ex) { Logger.Write(ex); return(false); } }
/// <summary> /// Deletes the EncryptedUserData entity from the database. /// </summary> /// <param name="userId"></param> /// <returns></returns> public bool Delete(string userId) { if (!String.IsNullOrEmpty(userId)) { using (SDContext sdContext = new SDContext()) { if (sdContext.EncryptedUserData.Get(x => x.UserId == userId) != null) { var user = sdContext.EncryptedUserData.Get(x => x.UserId == userId); sdContext.EncryptedUserData.Delete(user.Id); sdContext.Save(); return(true); } else { return(false); } } } else { Logger.Write("EncryptedDataUser entity hasn't been deleted.", "Check the method of Delete(string userId) in EncryptedUserBuilder.class"); return(false); } }
/// <summary> /// Saves the specified message. /// </summary> /// <param name="message"></param> /// <param name="exception"></param> /// <param name="extra"></param> /// <param name="userId"></param> /// <param name="url"></param> /// <param name="logType"></param> /// <param name="logTypeSub"></param> private static void Save(string message, string exception, string extra, string userId, string url, LogTypeEnum logType, LogTypeSubEnum logTypeSub) { try { using (SDContext context = new SDContext()) { SD.Data.Entities.Entities.Log log = new Data.Entities.Entities.Log { Id = Guid.NewGuid(), Date = DateTime.Now, Exception = exception, Message = message, Extra = extra, UserId = userId, LogTypeId = GetLogTypeGuid(logType), LogTypeSubId = GetLogTypeSubGuid(logTypeSub), Url = url }; context.Log.Insert(log); context.Save(); } } catch (Exception) { //no throw; } }
static void Main() { string menu = string.Empty; while (menu != "0") { Console.WriteLine("1. Инициализировать и заполнить БД\n0. Выход"); menu = Console.ReadKey().KeyChar.ToString(); Console.WriteLine(); switch (menu) { case "1": { var initializer = new DbInitializer(); Database.SetInitializer(initializer); using (var context = new SDContext()) { initializer.InitializeDatabase(context); } Console.WriteLine("Объекты успешно сохранены"); break; }; default: break; } ; } }
/// <summary> /// Gets a list of reports by teacherId. /// </summary> /// <param name="teacherId"></param> /// <returns></returns> public IEnumerable <ReportModel> GetList(Guid teacherId) { if (teacherId != null) { using (SDContext sdContext = new SDContext()) { return(sdContext.Report.GetList(x => x.Session.TeacherId == teacherId) .Select(x => new ReportModel() { Id = x.Id, Date = x.Date, Rating = x.Rating, Description = x.Description, CourseId = x.CourseId, SessionId = x.SessionId, Session = new SessionModel() { Id = x.Session.Id, ClassRoom = x.Session.Classroom, StartTime = x.Session.StartTime, EndTime = x.Session.EndTime, KlasId = x.Session.KlasId, CourseId = x.Session.CourseId, TeacherId = x.Session.TeacherId } })); } } else { return(null); } }
public Order GetOrderByID(int iD) { using (var context = new SDContext()) { return(context.Orders.Where(x => x.ID == iD).Include(x => x.OrderItems).Include("OrderItems.Product").FirstOrDefault()); } }
public List <Category> GetFeaturedCategories() { using (var context = new SDContext()) { return(context.Categories.Where(x => x.IsFeatured && x.ImageURL != null).ToList()); } }
public Category GetCategory(int ID) { using (var context = new SDContext()) { return(context.Categories.Find(ID)); } }
public int GetMaximumPrice() { using (var context = new SDContext()) { return((int)(context.Products.Max(x => x.Price))); } }
public List <Product> GetProductsByCategory(int categoryId, int pageSize) { using (var context = new SDContext()) { return(context.Products.Where(x => x.Category.Id == categoryId).OrderByDescending(x => x.Id).Take(pageSize).Include(x => x.Category).ToList()); } }
public List <Product> GetLatestProducts(int numberOfProducts) { using (var context = new SDContext()) { return(context.Products.OrderByDescending(x => x.Id).Take(numberOfProducts).Include(x => x.Category).ToList()); } }
public List <Product> GetProducts(int pageNo, int pageSize) { using (var context = new SDContext()) { return(context.Products.OrderByDescending(x => x.Id).Skip((pageNo - 1) * pageSize).Take(pageSize).Include(x => x.Category).ToList()); } }
public List <Product> GetProducts(string search, int pageNo, int pageSize) { using (var context = new SDContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Products.Where(product => product.Name != null && product.Name.ToLower().Contains(search.ToLower())) .OrderBy(x => x.Id) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Category) .ToList()); } else { return(context.Products .OrderBy(x => x.Id) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Category) .ToList()); } } }
public Product GetProduct(int Id) { using (var context = new SDContext()) { return(context.Products.Where(x => x.Id == Id).Include(x => x.Category).FirstOrDefault()); } }
public List <Product> GetProducts(List <int> Ids) { using (var context = new SDContext()) { return(context.Products.Where(product => Ids.Contains(product.Id)).ToList()); } }
/// <summary> /// Insert a new Encrypted user in the database. /// </summary> /// <param name="model"></param> /// <param name="userId"></param> /// <returns></returns> public bool Insert(RegisterViewModel model, string userId) { try { if (model != null && !string.IsNullOrEmpty(userId)) { using (SDContext sdContext = new SDContext()) { sdContext.EncryptedUserData.Insert(new EncryptedUserData { Id = Guid.NewGuid(), Email = HashData.ComputeHash(model.Email, "SHA256", null), Password = HashData.ComputeHash(model.Password, "SHA256", null), PhoneNumber = HashData.ComputeHash(model.PhoneNumber, "SHA256", null), UserId = userId }); sdContext.Save(); } return(true); } else { return(false); } } catch (Exception ex) { Logger.Write(ex); return(false); } }
public List <Category> GetCategories(string search, int pageNo) { int pageSize = 3; using (var context = new SDContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Categories.Where(category => category.Name != null && category.Name.ToLower().Contains(search.ToLower())) .OrderBy(x => x.Id) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Products) .ToList()); } else { return(context.Categories .OrderBy(x => x.Id) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Products) .ToList()); } } }
/// <summary> /// Checks if there's any teacher containing the same email as the login email input. /// </summary> /// <param name="email"></param> /// <returns></returns> public bool IsTeacher(string email) { try { if (!String.IsNullOrEmpty(email)) { using (SDContext sdContext = new SDContext()) { Teacher teacher = sdContext.Teacher.Get(x => x.Email == email); if (teacher != null && teacher.Email == email) { return(true); } else { return(false); } } } else { return(false); } } catch (Exception ex) { Logger.Write(ex); return(false); } }
/// <summary> /// Inserts a new SystemAdministrator to the database. /// </summary> /// <param name="model"></param> /// <param name="userId"></param> /// <returns></returns> public bool InsertAdministrator(RegisterViewModel model, string userId) { try { if (model != null && !String.IsNullOrEmpty(userId) && !String.IsNullOrEmpty(model.Email) && !String.IsNullOrEmpty(model.Password) && !String.IsNullOrEmpty(model.Firstname) && !String.IsNullOrEmpty(model.Lastname)) { using (SDContext sdContext = new SDContext()) { sdContext.Administrator.Insert(new Administrator { Id = Guid.NewGuid(), Email = model.Email, Firstname = model.Firstname, Lastname = model.Lastname, UserId = userId }); sdContext.Save(); } AssignAdministratorRole(userId); return(true); } else { Logger.Write("Administrator could not be added.", "The model validation returned false. This was caused in the method InsertAdministrator() in the class UserBuilder."); return(false); } } catch (Exception ex) { Logger.Write(ex); return(false); } }
/// <summary> /// Gets a list of Courses by teacherId. /// </summary> /// <param name="teacherId"></param> /// <returns></returns> public IEnumerable <CourseModel> GetList(Guid teacherId) { if (teacherId != null) { using (SDContext sdContext = new SDContext()) { var sessions = sdContext.Session.GetList(x => x.TeacherId == teacherId); var courses = new List <CourseModel>(); foreach (var co in sessions) { courses.Add(new CourseModel() { Id = co.Course.Id, Coordinator = co.Course.Coordinator, CourseName = co.Course.CourseName }); } return(courses); } } else { return(null); } }
/// <summary> /// Gets an Administrator entity by the specified email. /// </summary> /// <param name="email"></param> /// <returns></returns> public AdministratorModel GetAdministratorByEmail(string email) { if (!String.IsNullOrEmpty(email)) { using (SDContext sdContext = new SDContext()) { var admin = sdContext.Administrator.Get(x => x.Email == email); if (admin == null) { return(null); } AdministratorModel model = new AdministratorModel() { Id = admin.Id, Email = admin.Email, Firstname = admin.Firstname, Lastname = admin.Lastname, UserId = admin.UserId, LastLogin = admin.LastLogin }; return(model); } } else { return(null); } }
public List <Category> GetAllCategories() { using (var context = new SDContext()) { return(context.Categories .ToList()); } }
public void UpdateProduct(Product product) { using (var context = new SDContext()) { context.Entry(product).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void SaveCategory(Category category) { using (var context = new SDContext()) { context.Categories.Add(category); context.SaveChanges(); } }
public int SaveOrder(Order order) { using (var context = new SDContext()) { context.Orders.Add(order); return(context.SaveChanges()); } }
public void UpdateCategory(Category category) { using (var context = new SDContext()) { context.Entry(category).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }