public void SetupTest() { _db = DatabaseHelper.CreateNewDatabase(); user = new user { username = "******", first_name = "FirstName", middle_name = "MiddleName", last_name = "LastName", is_player = true, created_date = DateTime.Now, status = (int)JPPConstants.UserStatus.Active, first_login = true, email = "*****@*****.**", last_login_date = DateTime.Now, display_name = "DisplayName", privacy_settings = (int)JPPConstants.PrivacySettings.JustPressPlayOnly, has_agreed_to_tos = false, creator_id = null }; _db.user.Add(user); DatabaseHelper.TrySaveChanges(ref _db); }
public static JustPressPlayDBEntities CreateNewDatabase() { // Code derived from http://www.codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "")); // see http://stackoverflow.com/questions/12244495/how-to-set-up-localdb-for-unit-tests-in-visual-studio-2012-and-entity-framework/14680912#14680912 var filePath = Directory.GetCurrentDirectory() + @"\JustPressPlayDB.mdf"; var logFilePath = Directory.GetCurrentDirectory() + @"\JustPressPlayDB_log.ldf"; var origConnectionString=@"metadata=res://*/Models.JustPressPlayEF.csdl|res://*/Models.JustPressPlayEF.ssdl|res://*/Models.JustPressPlayEF.msl;provider=System.Data.SqlClient;provider connection string=""data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\JustPressPlayDB.mdf;integrated security=True;MultipleActiveResultSets=True;"""; if (File.Exists(filePath)) File.Delete(filePath); if (File.Exists(logFilePath)) File.Delete(logFilePath); string connectionString = origConnectionString;//"Datasource = " + filePath; Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); // Initialize DB using (var context = new JustPressPlayDBEntities(connectionString)) { context.Database.Create(); } // Connect to DB and return it return new JustPressPlayDBEntities(connectionString); }
public static void TrySaveChanges(ref JustPressPlayDBEntities db) { try { db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } throw dbEx; } }
public static void LogSingleEntry(LoggerModel loggerModel, JustPressPlayDBEntities _dbContext, bool autoSave = false) { log newLogEntry = new log() { action = loggerModel.Action, ip_address = loggerModel.IPAddress, id_type_1 = loggerModel.IDType1 == null ? null : loggerModel.IDType1, id_type_2 = loggerModel.IDType2 == null ? null : loggerModel.IDType2, id_1 = loggerModel.ID1 == null ? null : loggerModel.ID1, id_2 = loggerModel.ID2 == null ? null : loggerModel.ID2, timestamp = loggerModel.TimeStamp, user_id = loggerModel.UserID, value_1 = loggerModel.Value1 == null? null : loggerModel.Value1, value_2 = loggerModel.Value2 == null? null : loggerModel.Value2, }; _dbContext.log.Add(newLogEntry); if (autoSave) _dbContext.SaveChanges(); }
public static void LogMultipleEntries(List<LoggerModel> loggerModelList, JustPressPlayDBEntities _dbContext) { foreach (LoggerModel loggerModel in loggerModelList) { LogSingleEntry(loggerModel, _dbContext); } }
public static Earning SingleEarning(int id, bool isAchievement) { UnitOfWork work = new UnitOfWork(); JustPressPlayDBEntities _dbContext = new JustPressPlayDBEntities(); Earning earning = new Earning(); var loggedInID = WebSecurity.CurrentUserId; var loggedInIsAdmin = Roles.IsUserInRole(JPPConstants.Roles.FullAdmin); if (isAchievement) { var achievement = _dbContext.achievement_instance.Find(id); var user = achievement.user; var template = achievement.achievement_template; earning.CommentsDisabled = WebSecurity.IsAuthenticated ? achievement.comments_disabled : true; earning.DisplayName = user.display_name; earning.EarnedDate = achievement.achieved_date; earning.EarningID = achievement.id; earning.EarningIsAchievement = true; earning.Image = template.icon; earning.PlayerID = user.id; earning.PlayerImage = user.image; earning.TemplateID = template.id; earning.Title = template.title; if (achievement.has_user_content) { var content = _dbContext.achievement_user_content.Find(achievement.user_content_id); earning.ContentPhoto = content.image; earning.ContentText = content.text; earning.ContentURL = content.url; } if (achievement.has_user_story) { var story = _dbContext.achievement_user_story.Find(achievement.user_story_id); earning.StoryPhoto = story.image; earning.StoryText = story.text; } } else { var quest = _dbContext.quest_instance.Find(id); var user = quest.user; var template = quest.quest_template; earning.CommentsDisabled = WebSecurity.IsAuthenticated ? quest.comments_disabled : true; earning.DisplayName = user.display_name; earning.EarnedDate = quest.completed_date; earning.EarningID = quest.id; earning.EarningIsAchievement = false; earning.Image = template.icon; earning.PlayerID = user.id; earning.PlayerImage = user.image; earning.TemplateID = template.id; earning.Title = template.title; } // Get comments if (!earning.CommentsDisabled) { earning.Comments = from c in work.EntityContext.comment where c.location_id == earning.EarningID && ((earning.EarningIsAchievement && c.location_type == (int)JPPConstants.CommentLocation.Achievement) || (!earning.EarningIsAchievement && c.location_type == (int)JPPConstants.CommentLocation.Quest)) select new EarningComment() { ID = c.id, PlayerID = c.deleted ? c.last_modified_by_id : c.user_id, // Replace comment text if deleted and not admin Text = c.deleted ? (JPPConstants.SiteSettings.DeletedCommentText + c.last_modified_by.display_name) : c.text, PlayerImage = c.deleted ? null : c.user.image, DisplayName = c.deleted ? null : c.user.display_name, Deleted = c.deleted, CommentDate = c.date, CurrentUserCanEdit = (loggedInID == c.user_id || loggedInIsAdmin) && !c.deleted, CurrentUserCanDelete = (loggedInID == c.user_id || loggedInID == earning.PlayerID || loggedInIsAdmin) && !c.deleted }; } earning.CurrentUserCanAddStory = earning.EarningIsAchievement && loggedInID == earning.PlayerID; earning.CurrentUserCanEditStory = earning.EarningIsAchievement && ( loggedInID == earning.PlayerID || loggedInIsAdmin ); return earning; }