public static void AddImage(Image image, string tags, bool[] filters) { var entity = new ImageNetworkEntities(); if (tags != null) { string[] tagList = tags.Split(new Char[] { ' ', ',' }); foreach (var item in tagList) { if (item == "") continue; var tag = new Tag { Name = item, }; tag.Name = tag.Name.TrimEnd().ToLower(); if (entity.Tag.FirstOrDefault(a => a.Name == tag.Name) != null) { tag = entity.Tag.FirstOrDefault(a => a.Name == tag.Name); } image.Tag.Add(tag); entity.SaveChanges(); } for (int filterId = 1; filterId < filters.Length+1; filterId++) { if (filters[filterId - 1]) image.Filter.Add(entity.Filter.FirstOrDefault(a => a.Id == filterId)); } } entity.Image.Add(image); entity.SaveChanges(); }
public static void AddUser(User user) { var entity = new ImageNetworkEntities(); entity.User.Add(user); entity.SaveChanges(); }
public static void AddFilter(int imageId, int filterId) { var entity = new ImageNetworkEntities(); entity.Image.FirstOrDefault(a => a.Id == imageId).Filter.Add(entity.Filter.FirstOrDefault(a => a.Id == filterId)); entity.SaveChanges(); }
public static void ActivateUser(string email) { var entity = new ImageNetworkEntities(); entity.User.FirstOrDefault(a => a.Email == email).IsBlock = false; entity.SaveChanges(); }
public static void AddTag(Tag tag) { var entity = new ImageNetworkEntities(); tag.Name = tag.Name.TrimEnd().ToLower(); if (entity.Tag.FirstOrDefault(a => a.Name == tag.Name) == null) { entity.Tag.Add(tag); entity.SaveChanges(); } }
public static void DeleteImage(int id) { var entity = new ImageNetworkEntities(); Image image = entity.Image.FirstOrDefault(a => a.Id == id); MvcApplication1.Helpers.ImageHelper.DeleteFile(image.Picture); MvcApplication1.Helpers.ImageHelper.DeleteFile(image.SmallPicture); image.Tag.Clear(); UserImageRating[] ratings = entity.UserImageRating.Where(a => a.ImageId == id).ToArray(); foreach (var item in ratings) { entity.UserImageRating.Remove(item); } entity.Image.Remove(image); entity.SaveChanges(); }
public static void Rate(User user, Image image, int rating) { var entity = new ImageNetworkEntities(); var userImageRating = entity.UserImageRating.FirstOrDefault(a => a.UserId == user.Id && a.ImageId == image.Id); if (userImageRating != null) { if (userImageRating.Rating == rating) { entity.UserImageRating.Remove(userImageRating); entity.SaveChanges(); return; } else { entity.UserImageRating.Remove(userImageRating); } } var newRate = new UserImageRating { UserId = user.Id, ImageId = image.Id, Rating = rating, }; entity.UserImageRating.Add(newRate); entity.SaveChanges(); }