示例#1
0
        /// <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);
            }
        }
示例#2
0
        /// <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);
            }
        }
示例#3
0
 public PostRepository()
 {
     _context = new FriendshipContext();
 }
示例#4
0
 public UserRepository(FriendshipContext context, IConfiguration iconfig)
 {
     _context = context;
     config   = iconfig;
 }
 public UsersController(IUserRepository repo, IMapper mapper, FriendshipContext context)
 {
     _repo    = repo;
     _mapper  = mapper;
     _context = context;
 }