/// <summary> /// Initialize the service. /// </summary> public void Initialize() { try { using (var context = new FriendshipContext()) { var allFriendships = context.Objects.ToList(); foreach (var friendshipProxy in allFriendships) { var realFriendship = (FriendshipModel)friendshipProxy.CreateFromProxy(friendshipProxy); CacheOutgoingFriendship(realFriendship); CacheIncomingFriendship(realFriendship); } } $"Cached {incomingFriendships.Count} friendships".WriteToConsole(); logger.Info($"Initialized { nameof(FriendshipService) }"); } catch (Exception ex) { string message = $"Failed to initialize {nameof(FriendshipService)}"; message.WriteToConsole(); logger.Error(ex, message); } }
/// <summary> /// Save the friendship to database. /// </summary> /// <param name="friendship">Friendship model.</param> public void SaveFriendship(FriendshipModel friendship) { ValidateModel(friendship); using (var context = new FriendshipContext()) { var existingModel = context .Objects .FirstOrDefault(model => model.ParentId == friendship.ParentId && model.AcceptorAccountId == friendship.AcceptorAccountId); if (existingModel == null) { context.Objects.Add(friendship); } else { string message = $"Friendship between {existingModel.NavigationParent.Name} and { existingModel.AcceptorAccount.Name } already exists"; throw new InvalidOperationException(message); } context.SaveChanges(); CacheIncomingFriendship(friendship); CacheOutgoingFriendship(friendship); } }
public PostRepository() { _context = new FriendshipContext(); }
public UserRepository(FriendshipContext context, IConfiguration iconfig) { _context = context; config = iconfig; }
public UsersController(IUserRepository repo, IMapper mapper, FriendshipContext context) { _repo = repo; _mapper = mapper; _context = context; }