public static long LikePostById(long postId, long?userId) { using (var context = new DAL.instDBEntities()) { var likeDB = context.Likes.FirstOrDefault(x => x.PostId == postId && x.UserId == userId); //если такого лайка нет в базе if (likeDB == null) { likeDB = context.Likes.Add(new DAL.Likes()); likeDB.Date = DateTime.Now; likeDB.UserId = userId.Value; likeDB.PostId = postId; var resss = context.SaveChanges(); return(likeDB.Id); } else { context.Likes.Remove(likeDB); context.SaveChanges(); return(0); } } }
public static bool CreateUpdateSubscription(long userId, long subTo) { //true - added sub //false - unsubed using (var ctx = new DAL.instDBEntities()) { bool result; var subDB = ctx.Subscribes.FirstOrDefault(x => x.UserId == userId && x.FollowingId == subTo); if (subDB == null) { subDB = ctx.Subscribes.Add(new DAL.Subscribes { UserId = userId, Date = DateTime.Now, FollowingId = subTo, isActive = true }); result = true; } else { //если подписка существует в базе, то меняем её статус на противоположный subDB.isActive = !subDB.isActive; result = subDB.isActive; } ctx.SaveChanges(); return(result); } }
public static void DeletePostDB(long id) { using (var ctx = new DAL.instDBEntities()) { var post = ctx.Posts.FirstOrDefault(x => x.Id == id); if (post == null) { return; } var likes = ctx.Likes.Where(x => x.PostId == post.Id).ToList(); var comments = ctx.Comments.Where(x => x.PostId == post.Id).ToList(); var photos = ctx.Photos.Where(x => x.PostId == post.Id).ToList(); foreach (var like in likes) { ctx.Likes.Remove(like); } foreach (var comment in comments) { ctx.Comments.Remove(comment); } foreach (var photo in photos) { ctx.Photos.Remove(photo); } ctx.Posts.Remove(post); ctx.SaveChanges(); } }
public static long CreateOrUpdateUser(UserDTO user) { using (var context = new DAL.instDBEntities()) { var userDB = context.Users.FirstOrDefault(x => x.Id == user.Id) ?? context.Users.Add(new DAL.Users()); if (context.Users.Any(x => x.Nickname == user.Nickname && x.Id != userDB.Id)) { throw new Exception($"Никнейм {userDB.Nickname} занят"); } userDB.BirthDate = user.BirthDate; userDB.Description = user.Description; userDB.Login = user.Login; userDB.Nickname = user.Nickname; userDB.PasswordHash = user.PasswordHash; userDB.RegDate = user.RegDate; userDB.Salt = user.Salt; userDB.SharedProfile = user.SharedProfile; context.SaveChanges(); return(userDB.Id); } }
public static long?CreateUpdatePost(PostDTO post) { using (var ctx = new DAL.instDBEntities()) { var imgIds = post.Photos.Select(x => x.Id).ToArray(); var images = ctx.Photos.Where(x => imgIds.Contains(x.Id)).ToList(); var dbPost = new DAL.Posts { UserId = post.UserId, Description = post.Description, LocationName = post.LocationName, }; dbPost.Photos.Clear(); images.ForEach((x) => dbPost.Photos.Add(x)); dbPost.LoadDate = DateTime.Now; dbPost.PublicateDate = DateTime.Now; ctx.Posts.Add(dbPost); ctx.SaveChanges(); return(dbPost.Id); } }
public static void UpdatePhotoPost(DAL.Photos photo, long postId) { using (var ctx = new DAL.instDBEntities()) { photo.PostId = postId; ctx.SaveChanges(); } }
public static void DelImageDB(long ImageId) { using (var ctx = new DAL.instDBEntities()) { var img = ctx.Photos.FirstOrDefault(x => x.Id == ImageId); if (img == null) { throw new Exception("Нет такой фотки"); } ctx.Photos.Remove(img); ctx.SaveChanges(); } }
public static void SetAvatar(long UserId, ImageContent avatar) { using (var ctx = new DAL.instDBEntities()) { var user = ctx.Users.FirstOrDefault(x => x.Id == UserId); if (user == null) { throw new Exception("User not found"); } user.AvatarContent = avatar.Content; user.AvatarMime = avatar.Mime; ctx.SaveChanges(); } }
public static long AddPhoto(long UserId, ImageContent ImageData) { using (var ctx = new DAL.instDBEntities()) { var img = ctx.Photos.Add(new DAL.Photos { UserId = UserId, LoadDate = DateTime.Now, ImageContent = ImageData.Content, MimeType = ImageData.Mime, }); ctx.SaveChanges(); return(img.Id); } }
public static long CreateCommentDB(CommentDTO comment) { using (var ctx = new DAL.instDBEntities()) { var dbCom = new DAL.Comments { Id = comment.Id, UserId = comment.UserId, PostId = comment.PostId, Date = comment.Date, ContentText = comment.ContentText }; ctx.Comments.Add(dbCom); ctx.SaveChanges(); return(dbCom.Id); } }