Ejemplo n.º 1
0
        public virtual async void TestDelete()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);
            var        client     = new ApiClient(testServer.CreateClient());

            client.SetBearerToken(JWTTestHelper.GenerateBearerToken());
            ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;

            IBadgeService service = testServer.Host.Services.GetService(typeof(IBadgeService)) as IBadgeService;
            var           model   = new ApiBadgeServerRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1);
            CreateResponse <ApiBadgeServerResponseModel> createdResponse = await service.Create(model);

            createdResponse.Success.Should().BeTrue();

            ActionResponse deleteResult = await client.BadgeDeleteAsync(2);

            deleteResult.Success.Should().BeTrue();
            ApiBadgeServerResponseModel verifyResponse = await service.Get(2);

            verifyResponse.Should().BeNull();
        }
Ejemplo n.º 2
0
        public virtual async void TestUpdate()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);

            var client = new ApiClient(testServer.CreateClient());

            client.SetBearerToken(JWTTestHelper.GenerateBearerToken());
            var mapper = new ApiBadgeServerModelMapper();
            ApplicationDbContext        context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
            IBadgeService               service = testServer.Host.Services.GetService(typeof(IBadgeService)) as IBadgeService;
            ApiBadgeServerResponseModel model   = await service.Get(1);

            ApiBadgeClientRequestModel request = mapper.MapServerResponseToClientRequest(model);

            request.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1);

            UpdateResponse <ApiBadgeClientResponseModel> updateResponse = await client.BadgeUpdateAsync(model.Id, request);

            context.Entry(context.Set <Badge>().ToList()[0]).Reload();
            updateResponse.Record.Should().NotBeNull();
            updateResponse.Success.Should().BeTrue();
            updateResponse.Record.Id.Should().Be(1);
            context.Set <Badge>().ToList()[0].Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM"));
            context.Set <Badge>().ToList()[0].Name.Should().Be("B");
            context.Set <Badge>().ToList()[0].UserId.Should().Be(1);

            updateResponse.Record.Id.Should().Be(1);
            updateResponse.Record.Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM"));
            updateResponse.Record.Name.Should().Be("B");
            updateResponse.Record.UserId.Should().Be(1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Make a badge activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        private BadgeActivity GenerateBadgeActivity(Activity activity)
        {
            //System.Reflection.MethodBase.GetCurrentMethod().Name
            var cacheKey = string.Concat(CacheKeys.Activity.StartsWith, "GenerateBadgeActivity-", activity.GetHashCode());

            return(_cacheService.CachePerRequest(cacheKey, () =>
            {
                // Get the corresponding badge
                var dataPairs = ActivityBase.UnpackData(activity);

                if (!dataPairs.ContainsKey(BadgeActivity.KeyBadgeId))
                {
                    // Log the problem then skip
                    _loggingService.Error($"A badge activity record with id '{activity.Id}' has no badge id in its data.");
                    return null;
                }

                var badgeId = dataPairs[BadgeActivity.KeyBadgeId];
                var badge = _badgeService.Get(new Guid(badgeId));

                if (badge == null)
                {
                    // Log the problem then skip
                    _loggingService.Error($"A badge activity record with id '{activity.Id}' has a badge id '{badgeId}' that is not found in the badge table.");
                    return null;
                }

                var userId = dataPairs[BadgeActivity.KeyUserId];
                var user = _context.MembershipUser.FirstOrDefault(x => x.Id == new Guid(userId));

                if (user == null)
                {
                    // Log the problem then skip
                    _loggingService.Error($"A badge activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table.");
                    return null;
                }

                return new BadgeActivity(activity, badge, user);
            }));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Make a badge activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        private BadgeActivity GenerateBadgeActivity(Activity activity)
        {
            // Get the corresponding badge
            var dataPairs = ActivityBase.UnpackData(activity);

            if (!dataPairs.ContainsKey(BadgeActivity.KeyBadgeId))
            {
                // Log the problem then skip
                _loggingService.Error(
                    $"A badge activity record with id '{activity.Id}' has no badge id in its data.");
                return(null);
            }

            var badgeId = dataPairs[BadgeActivity.KeyBadgeId];
            var badge   = _badgeService.Get(new Guid(badgeId));

            if (badge == null)
            {
                // Log the problem then skip
                _loggingService.Error(
                    $"A badge activity record with id '{activity.Id}' has a badge id '{badgeId}' that is not found in the badge table.");
                return(null);
            }

            var userId = dataPairs[BadgeActivity.KeyUserId];
            var user   = _context.MembershipUser.FirstOrDefault(x => x.Id == new Guid(userId));

            if (user == null)
            {
                // Log the problem then skip
                _loggingService.Error(
                    $"A badge activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table.");
                return(null);
            }

            return(new BadgeActivity(activity, badge, user));
        }
Ejemplo n.º 5
0
 public async Task <ServiceResponse <List <Badge> > > Get()
 {
     return(await _badgeService.Get());
 }