private async Task Process(string fid, string com)
        {
            // NOTE: Throw is null, only for now (should log error later is dest and flight are both null)
            if (string.IsNullOrWhiteSpace(fid))
            {
                throw new NotImplementedException();
            }

            int flightId;
            int.TryParse(fid, out flightId);

            if (flightId < 1)
            {
                throw new NotImplementedException();
            }

            decimal commission;
            decimal.TryParse(com, out commission);

            // Model
            var conversion = new DatabaseConversion
            {
                AddedDate = DateTimeOffset.Now,
                // TODO: Shit
                Flight = flightId < 1 ? (int?)null : flightId,
                Destination = null,
                // TODO: Shit
                Value = commission == decimal.Zero ? (decimal?)null : commission
            };

            using (var sql = new SqlConnection(_conn))
            {
                await sql.OpenAsync();//.ConfigureAwait(false);

                var response = await sql.InsertAsync(conversion);//.ConfigureAwait(false);
                if (response < 1)
                {
                    // TODO: Log error
                }

                sql.Close();
            }

            // SignalR
            // TODO: [Optimization] If there are no clients connected to SignalR, don't push event message.
            // TODO: Refactor to own method (singleton or simpleinjector).
            _conversionHubContext.Clients
                                 .All
                                 .newConversion(new EmberConversion
                                 {
                                     Id = conversion.Id,
                                     AddedDate = conversion.AddedDate,
                                     Flight = conversion.Flight,
                                     Destination = conversion.Destination,
                                     Value = conversion.Value
                                 });
        }
Beispiel #2
0
        // TODO: There is no sanitization on message data.
        private async Task ProcessAdvertClick(Event evnt)
        {
            Trace.WriteLine("ProcessEvent");

            int result;

            // Database
            // TODO: Refactor to own method.
            // TODO: Automapper.
            // TODO: SQL connection shouldn't be initialized this way.
            using (var sql = new SqlConnection(_conn))
            {
                await sql.OpenAsync();//.ConfigureAwait(false);

                // TODO: Sanitize this input (the default '0' for null isn't good)
                // TODO: Should handle bad campaign or flight ids
                // TODO: Parse methods will throw on error, convert to TryParse.
                // TODO: Events should be isolated into it's own DB
                result = await sql.InsertAsync(new DatabaseEvent
                {
                    Time = evnt.Time.FromUnixTimeMilliseconds(),

                    UserId = Guid.Parse(evnt.UserId),
                    UserAgent = evnt.UserAgent,
                    UserLanguage = evnt.UserLanguage,
                    UserHostAddress = evnt.UserHostAddress == null ? null : IPAddress.Parse(evnt.UserHostAddress).GetAddressBytes(),
                    UserProxyAddress = evnt.UserProxyAddress == null ? null : IPAddress.Parse(evnt.UserProxyAddress).GetAddressBytes(),

                    Referer = evnt.Referer,

                    Flight = string.IsNullOrWhiteSpace(evnt.Flight) ? 0 : int.Parse(evnt.Flight),
                    Destination = string.IsNullOrWhiteSpace(evnt.Destination) ? 0 : int.Parse(evnt.Destination)
                });//.ConfigureAwait(false);

                sql.Close();
            }

            if (result < 1)
            {
                // TODO: Log error and raise hell
                throw new NotImplementedException();
            }

            // SignalR
            // TODO: [Optimization] If there are no clients connected to SignalR, don't push event message.
            // TODO: Refactor to own method (singleton or simpleinjector).
            // TODO: Automapper(?).
            // BUG: Needs to send click cost (should cache flights in memory and pull click cost [for speed])
            Trace.WriteLine("Sending _eventHubContext");
            _eventHubContext.Clients
                            .All
                            .newEvent(new EmberEvent
                            {
                                Id = result,

                                Time = evnt.Time.FromUnixTimeMilliseconds(),

                                UserId = evnt.UserId,
                                UserAgent = evnt.UserAgent,
                                UserLanguage = evnt.UserLanguage,
                                UserHostAddress = evnt.UserHostAddress,
                                UserProxyAddress = evnt.UserProxyAddress,

                                // TODO: Fix this, should pull from the Flight
                                UserClickCost = 0.25M,

                                Referer = evnt.Referer,

                                Flight = evnt.Flight,
                                Destination = evnt.Destination
                            });
            Trace.WriteLine("Sent _eventHubContext");
        }
        public async Task<CreateUserResult> CreateUserAsync(ApplicationUser user)
        {
            int? id = 0;
            string error = null;

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                int existingId = await connection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM dbo.Users WHERE Email=@Email", new {Email = user.Email});
                if (existingId == 0)
                {
                    id = await connection.InsertAsync(user);
                }
                else
                {
                    error = "Email address already exists.";
                }
            }

            if (id > 0)
            {
                return new CreateUserResult {IsSuccess = true};
            }

            return new CreateUserResult {IsSuccess = false, ErrorMessage = error ?? "Error inserting user."};
        }