public SuccessfullOperated<FriendshipResponseModel> DeleteFriendship(string firstUserId, string secondUserId) { var modelFromDb = this.data.Friendships .All() .FirstOrDefault(f => (f.FirstUserId == firstUserId && f.SecondUserId == secondUserId)); var successfullOperated = new SuccessfullOperated<FriendshipResponseModel> { IsSuccessfull = false, Element = null }; try { data.Friendships.Delete(modelFromDb); data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; } catch (Exception) { return successfullOperated; } }
public SuccessfullOperated<FriendshipResponseModel> AproveFriendship(string firstUserId, string secondUserId) { var successfullOperated = new SuccessfullOperated<FriendshipResponseModel>() { IsSuccessfull = false, Element = null }; var modelFromDb = this.data.Friendships .All() .FirstOrDefault(f => (f.FirstUserId == firstUserId && f.SecondUserId == secondUserId)); if (modelFromDb == null) { return successfullOperated; } modelFromDb.IsApproved = true; modelFromDb.Date = DateTime.Now; modelFromDb.ModifiedOn = DateTime.Now; this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; }
public SuccessfullOperated<MessageResponseModel> Delete(int id) { var model = this.data.Messages .All() .FirstOrDefault(m => m.Id == id); var successfullOperated = new SuccessfullOperated<MessageResponseModel> { IsSuccessfull = false, Element = null }; try { this.data.Messages.Delete(model); this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; } catch (Exception) { return successfullOperated; } }
public SuccessfullOperated<List<FriendshipResponseModel>> GetAllRequestFriendships(string userId) { var successfullOperated = new SuccessfullOperated<List<FriendshipResponseModel>>() { IsSuccessfull = false, Element = new List<FriendshipResponseModel>() }; var friends = this.data.Friendships .All() .Where(f => !f.IsApproved && f.IsFirstUserSender && f.SecondUserId == userId) .Select(FriendshipResponseModel.FromFriendship) .ToList(); successfullOperated.Element.AddRange(friends); successfullOperated.IsSuccessfull = true; return successfullOperated; }
public SuccessfullOperated<FriendshipResponseModel> Update(int id, FriendshipResponseModel model) { var successfullOperated = new SuccessfullOperated<FriendshipResponseModel>() { IsSuccessfull = false, Element = model }; var modelFromDb = this.data.Friendships .All() .FirstOrDefault(m => m.Id == id); if (modelFromDb == null) { return successfullOperated; } if (!string.IsNullOrEmpty(model.FirstUserId)) { modelFromDb.FirstUserId = model.FirstUserId; } if (!string.IsNullOrEmpty(model.SecondUserId)) { modelFromDb.SecondUserId = model.SecondUserId; } modelFromDb.IsApproved = model.IsApproved; data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; }
public SuccessfullOperated<FriendshipResponseModel> RequestFriendship(FriendshipResponseModel model) { var successfullOperated = new SuccessfullOperated<FriendshipResponseModel>() { IsSuccessfull = false, Element = model }; var firstUser = this.data.Users .All() .FirstOrDefault(f => f.Id == model.FirstUserId); if (firstUser == null) { return successfullOperated; } var secondUser = this.data.Users .All() .FirstOrDefault(f => f.Id == model.SecondUserId); if (secondUser == null) { return successfullOperated; } var friendshipForDb = new Friendship() { FirstUserId = model.FirstUserId, FirstUser = firstUser, SecondUserId = model.SecondUserId, SecondUser = secondUser, IsApproved = false, CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, IsDeleted = false, IsFirstUserSender = true }; try { this.data.Friendships.Add(friendshipForDb); this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; } catch (Exception) { return successfullOperated; } }
public SuccessfullOperated<FileRequestModel> SendFileToFriendship(int friendshipId, string authorId, FileRequestModel model) { var successfullOperated = new SuccessfullOperated<FileRequestModel>() { IsSuccessfull = false, Element = model }; var friendship = this.data.Friendships .All() .FirstOrDefault(f => f.Id == friendshipId); if (friendship == null) { return successfullOperated; } var author = this.data.Users .All() .FirstOrDefault(u => u.Id == authorId); if (author == null) { return successfullOperated; } var fileForDb = new File() { Author = model.Author, CompressedFile = model.CompressedFile }; try { model.Id = fileForDb.Id; friendship.Files.Add(fileForDb); this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; } catch (Exception) { return successfullOperated; } }
public SuccessfullOperated<MessageResponseModel> Update(int id, MessageResponseModel model) { var successfullOperated = new SuccessfullOperated<MessageResponseModel>() { IsSuccessfull = false, Element = model }; var modelFromDb = this.data.Messages .All() .FirstOrDefault(m => m.Id == id); if (modelFromDb == null) { return successfullOperated; } if (model.Author != null) { modelFromDb.Author = model.Author; } if (!string.IsNullOrEmpty(model.Content)) { modelFromDb.Content = model.Content; } if (model.FriendshipId != modelFromDb.FriendshipId && model.FriendshipId != 0) { modelFromDb.FriendshipId = model.FriendshipId; } this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; }
public SuccessfullOperated<MessageRequestModel> SendMessageToFriendship(int friendshipId, string authorId, MessageRequestModel model) { var successfullOperated = new SuccessfullOperated<MessageRequestModel>() { IsSuccessfull = false, Element = model }; var friendship = this.data.Friendships .All() .FirstOrDefault(f => f.Id == friendshipId); if (friendship == null) { return successfullOperated; } var author = this.data.Users .All() .FirstOrDefault(u => u.Id == authorId); if (author == null) { return successfullOperated; } var messageForDb = new Message() { Author = author, Content = model.Content, SentOn = DateTime.Now }; try { model.Id = messageForDb.Id; friendship.Messages.Add(messageForDb); this.data.SaveChanges(); successfullOperated.IsSuccessfull = true; return successfullOperated; } catch (Exception) { return successfullOperated; } }