Beispiel #1
0
        /// <summary>
        /// Save a friend request to database.
        /// </summary>
        /// <param name="request">Incoming friend request model.</param>
        /// <returns>Saved complex request model.</returns>
        public ComplexFriendRequest Save(FriendRequest request)
        {
            RegistrationService service   = RegistrationService.Instance;
            ComplexUserAccount  requester = service.FindById(request.RequesterAccount.Id);
            ComplexUserAccount  target    = service.FindById(request.TargetAccount.Id);

            using (var context = new ComplexFriendRequestContext())
            {
                ComplexFriendRequest savedRequest = context.Objects.FirstOrDefault(req => req.Id == request.Id);

                if (savedRequest == null)
                {
                    var complexRequest = new ComplexFriendRequest
                    {
                        Id           = request.Id,
                        ParentId     = requester.Id,
                        TargetId     = target.Id,
                        CreationDate = request.CreationDate,
                        RequestState = FriendRequestState.AwaitingUserAnswer
                    };

                    context.Objects.Add(complexRequest);

                    context.SaveChanges();

                    return(complexRequest);
                }

                return(savedRequest);
            }
        }
Beispiel #2
0
        private static IEnumerable <ComplexFriendRequest> SearchRequests(Func <ComplexFriendRequest, bool> selector)
        {
            using (var context = new ComplexFriendRequestContext())
            {
                var result =
                    context
                    .Objects
                    .Include(request => request.TargetAccount)
                    .Include(request => request.NavigationParent)
                    .Where(selector)
                    .ToList();

                return(result);
            }
        }
Beispiel #3
0
        /// <inheritdoc />
        /// <summary>
        /// Initialize the service.
        /// </summary>
        public void Initialize()
        {
            try
            {
                using (var dbContext = new ComplexFriendRequestContext())
                {
                    var friendRequest = dbContext.Objects.FirstOrDefault();

                    var test = friendRequest?.Id;
                }
            }
            catch (Exception ex)
            {
                ex.Message.WriteToConsole(ConsoleColor.Red);
                logger.Error(ex, $"Failed to initialize { nameof(FriendRequestService) } service");
            }
        }
Beispiel #4
0
        private static ComplexFriendRequest SaveReplyState(FriendRequestReply reply)
        {
            using (var context = new ComplexFriendRequestContext())
            {
                var targetRequest = context.Objects.FirstOrDefault(request => request.Id == reply.Request.Id);

                if (targetRequest == null)
                {
                    throw new InvalidOperationException($"Friend request for reply with id {reply.Request.Id} not found");
                }

                if (reply.Reaction == FriendRequestReaction.Accept)
                {
                    targetRequest.RequestState = FriendRequestState.Accepted;
                }
                else if (reply.Reaction == FriendRequestReaction.Decline)
                {
                    targetRequest.RequestState = FriendRequestState.Declined;
                }
                else if (reply.Reaction == FriendRequestReaction.Ban)
                {
                    //TODO wut?
                }
                else
                {
                    // not ready yet
                    throw new NotImplementedException();
                }

                targetRequest.DecisionDate = DateTime.Now;

                context.SaveChanges();

                return(targetRequest);
            }
        }