public void CreateOre(Ore ore) { using (var pooledRedisClient = new PooledRedisClientManager()) using (var redis = pooledRedisClient.GetClient()) { var redisOres = redis.As<Ore>(); if(ore.Id == 0) { ore.Id = (int)redisOres.GetNextSequence(); } redisOres.Store(ore); // Misc functions redis.SetValue(ore.Name + ":id", ore.Id.ToString()); var set = redis.Sets["Ores"]; set.Add(ore.Name); } }
public ActionResult Edit(Ore ore) { RedisUserRepository redisUser = new RedisUserRepository(); RedisOreRepository redisOre = new RedisOreRepository(); User author = JsonConvert.DeserializeObject<User>(Context.Session.GetString("user")); // Validation Checks List<string> validation = new List<string>(); if (ore.Name == null) { validation.Add("Please name your ore"); } if (ore.Description == null) { validation.Add("Please set a description of your ore"); } if (redisOre.ReadName(ore.Name) != null) // If the name is used in the database { if(author.OresAuthored != null) // If the author has authored ores { if (!author.OresAuthored.Contains(ore.Name)) // Ores Authored is populated but does not contain the ore { validation.Add("You do not have permission to edit this ore"); } // No else because if the author.OresAuthored contains the ore, it's good to go } else // Ores Authored is empty { validation.Add("You do not have permission to edit this ore"); } } ViewData["validationSummary"] = validation; if (!validation.Any()) { if(ore.Comments == null) { List<int> comments = new List<int>(); ore.Comments = comments; } // Save ore to DB ore.AuthorId = author.Id; redisOre.UpdateOre(ore); if (author.OresAuthored == null) { List<string> list = new List<string>(); list.Add(ore.Name); author.OresAuthored = list; } else { if(!author.OresAuthored.Contains(ore.Name)) { author.OresAuthored.Add(ore.Name); // If the ore isn't already in the author's list } } redisUser.UpdateUser(author); // Save ore to App Data return RedirectToAction("Profile", "Community"); } else { // Model is not valid, return view to the user return View(ore); } }
public Ore ReadOre(int oreId) { Ore ore = new Ore(); using (var pooledRedisClient = new PooledRedisClientManager()) using (var redis = pooledRedisClient.GetClient()) { Ore target = redis.GetById<Ore>(oreId); return target; } }
public void UpdateOre(Ore ore) { using (var pooledRedisClient = new PooledRedisClientManager()) using (var redis = pooledRedisClient.GetClient()) { var redisOre = redis.As<Ore>(); if (ore.Id != 0) { redisOre.DeleteById(ore.Id); // Delete old user using ID redisOre.Store(ore); } else { ore.Id = (int)redisOre.GetNextSequence(); redisOre.Store(ore); // Misc functions redis.SetValue(ore.Name + ":id", ore.Id.ToString()); var set = redis.Sets["Ores"]; set.Add(ore.Name); } } }