public string Add(int userId, MessageCreateNewRequest request)
        {
            if (userId == request.ReceiverID)
            {
                return(Error.CANNOT_MESSAGE_YOURSELF);
            }

            if (_context.UserEntity.FirstOrDefault(x => x.ID == userId) == null)
            {
                return(string.Format(Error.CANNOT_FIND_USER, userId));
            }
            if (_context.UserEntity.FirstOrDefault(x => x.ID == request.ReceiverID) == null)
            {
                return(string.Format(Error.CANNOT_FIND_USER, request.ReceiverID));
            }

            _context.MessageEntity.Add(new MessageEntity
            {
                SenderID                 = userId,
                ReceiverID               = request.ReceiverID,
                SenderEncryptedContent   = request.Content,               //TODO: encrypt
                ReceiverEncryptedContent = request.Content,               //TODO: encrypt
                SentDateTime             = DateTime.UtcNow,
                SeenByReceiver           = false
            });
            _context.SaveChanges();
            return(Error.OK);
        }
        public string Add(UserCreateNewRequest request)
        {
            if (request.Login == null || request.Password == null)
            {
                return(Error.LOGIN_OR_PW_NULL);
            }

            if (_context.UserEntity.FirstOrDefault(x => x.Login == request.Login) != null)
            {
                return(string.Format(Error.LOGIN_EXIST, request.Login));
            }
            if (_context.UserEntity.FirstOrDefault(x => x.Email == request.Email) != null)
            {
                return(string.Format(Error.EMAIL_EXIST, request.Email));
            }

            int    random = new Random().Next();
            string pw     = request.Password + random.ToString();

            _context.UserEntity.Add(new UserEntity
            {
                Login        = request.Login,
                Email        = request.Email,
                PasswordHash = Hash(pw),
                Salt         = random,
                PublicKey    = "???"              //TODO: generate from password = private key
            });
            _context.SaveChanges();

            SendWelcomeEmail(request.Email);

            return(Error.OK);
        }
        private PaymentResponse AddPayment(int userId, PaymentRequest request)
        {
            var payment = new PaymentEntity
            {
                Amount   = request.Amount,
                Currency = _currency,
                DateTime = DateTime.UtcNow,
                Status   = (int)PaymentEntity.Statuses.PENDING,
                UserID   = userId
            };

            _context.PaymentEntity.Add(payment);
            _context.SaveChanges();

            return(new PaymentResponse
            {
                ID = payment.ID,
                Response = Error.OK
            });
        }
Esempio n. 4
0
        public string Add(FriendRelationRequest request)
        {
            if (request.FriendListOwnerID == request.FriendID)
            {
                return(Error.CANNOT_ADD_YOURSELF);
            }

            var relations = FindRelations(request);

            if (relations.Count == 2)
            {
                return(Accept(request));
            }

            if (_context.UserEntity.FirstOrDefault(x => x.ID == request.FriendID) == null)
            {
                return(string.Format(Error.CANNOT_FIND_USER, request.FriendID));
            }
            if (_context.UserEntity.FirstOrDefault(x => x.ID == request.FriendListOwnerID) == null)
            {
                return(string.Format(Error.CANNOT_FIND_USER, request.FriendListOwnerID));
            }

            _context.FriendRelationEntity.Add(new FriendRelationEntity
            {
                FriendListOwnerID = request.FriendListOwnerID,
                FriendID          = request.FriendID,
                Accepted          = true
            });
            _context.FriendRelationEntity.Add(new FriendRelationEntity
            {
                FriendListOwnerID = request.FriendID,
                FriendID          = request.FriendListOwnerID,
                Accepted          = false
            });
            _context.SaveChanges();
            return(Error.OK);
        }