Пример #1
0
        public async Task <IActionResult> CreateAsync([FromBody] Client.Entry.EntryBuildInfo entryBuildInfo,
                                                      CancellationToken cancellationToken)
        {
            if (entryBuildInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("Entry");
                return(this.BadRequest(error));
            }
            var userId = User.FindFirstValue("userId");

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(this.BadRequest(error));
            }

            var   entryCreationInfo = new EntryCreationInfo(userId, entryBuildInfo.ActivityId);
            Entry modelEntry;

            try
            {
                modelEntry = await entryRepository.CreateAsync(entryCreationInfo, cancellationToken);
            }
            catch (EntryDuplicationException e)
            {
                return(BadRequest(e.Message));
            }

            var clientEntry = EntryConverter.Convert(modelEntry);

            return(CreatedAtRoute(
                       "GetEntry",
                       new { id = clientEntry.Id },
                       clientEntry));
        }
Пример #2
0
        public async Task <IActionResult> CreateAsync([FromBody] Client.Maraphone.MaraphoneBuildInfo buildInfo,
                                                      CancellationToken cancellationToken)
        {
            if (buildInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("MaraphoneBuildInfo");
                return(this.BadRequest(error));
            }
            var userId = User.FindFirstValue("userId");

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(this.BadRequest(error));
            }

            var maraphoneCreationInfo = new MaraphoneCreationInfo(buildInfo, userId);

            var modelMaraphone = await maraphoneRepository.CreateAsync(maraphoneCreationInfo, cancellationToken);

            var clientMaraphone = MaraphoneConverter.Convert(modelMaraphone);

            return(CreatedAtRoute(
                       "GetMaraphone",
                       new { id = clientMaraphone.Id },
                       clientMaraphone));
        }
Пример #3
0
        public async Task <IActionResult> UpdateAsync([FromBody] Client.Users.UserPatchInfo patchInfo, CancellationToken cancellationToken)
        {
            if (patchInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("UserPatchInfo");
                return(this.BadRequest(error));
            }

            var userId = User.FindFirstValue("userId");

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(this.BadRequest(error));
            }

            User modelUser;

            try
            {
                modelUser = await userRepository.PatchAsync(patchInfo, userId, cancellationToken);
            }
            catch (UserNotFoundException e)
            {
                var error = ServiceErrorResponses.NoSuchObject("User", e.Message);
                return(this.NotFound(error));
            }

            var clientUserInfo = UserConverter.ConvertToUserInfo(modelUser);

            return(Ok(clientUserInfo));
        }
Пример #4
0
        public async Task <IActionResult> DeleteAsync()
        {
            var userId = User.FindFirstValue("userId");

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(this.BadRequest(error));
            }

            await userRepository.RemoveAsync(userId);

            return(NoContent());
        }
Пример #5
0
        public async Task <IActionResult> CreateAsync([FromBody] Client.ActivityBuildInfo buildInfo,
                                                      CancellationToken cancellationToken)
        {
            if (buildInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("ActivityCreationInfo");
                return(this.BadRequest(error));
            }
            var userId = User.FindFirstValue("userId");

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(this.BadRequest(error));
            }

            var maraphoneId = buildInfo.MaraphoneId;
            var maraphone   = await maraphoneRepository.GetAsync(maraphoneId, cancellationToken);

            if (maraphone == null)
            {
                var error = ServiceErrorResponses.NoSuchObject("Maraphone", "Maraphone not found");
                return(this.NotFound(error));
            }

            var duration = maraphone.Duration;
            var endAt    = buildInfo.StartAt + duration;

            var activityCreationInfo = new ActivityCreationInfo(buildInfo.MaraphoneId, buildInfo.Tags,
                                                                userId, buildInfo.Experts, buildInfo.StartAt, endAt);

            var modelActivity = await activityRepository.CreateAsync(activityCreationInfo, endAt, cancellationToken);

            var clientActivity = ActivityConverter.Convert(modelActivity);

            return(CreatedAtRoute(
                       "GetActivity",
                       new { id = clientActivity.Id },
                       clientActivity));
        }
Пример #6
0
        public async Task <IActionResult> Refresh()
        {
            var token = HttpContext.Request.Headers["Authorization"].FirstOrDefault()?.Substring(7);

            var principal    = authenticator.GetPrincipalFromExpiredToken(token);
            var userId       = principal.Claims.First(claim => claim.Type == "userId").Value;
            var refreshToken = principal.Claims.First(claim => claim.Type == "refreshToken").Value;

            if (userId == null)
            {
                var error = ServiceErrorResponses.InvalidClaims("userId");
                return(BadRequest(error));
            }

            var refreshTokenExist = await tokenRepository.IsRefreshTokenExistAsync(userId, refreshToken);

            if (!refreshTokenExist)
            {
                return(BadRequest("Invalid refresh token"));
            }

            var newRefreshToken = authenticator.GenerateRefreshToken();
            var claims          = new List <Claim>
            {
                new Claim("userId", userId),
                new Claim("refreshToken", newRefreshToken)
            };
            var newJwtToken = GenerateToken(claims);
            await tokenRepository.RemoveRefreshTokenAsync(userId, refreshToken);

            await tokenRepository.SaveRefreshTokenAsync(userId, newRefreshToken);

            return(new ObjectResult(new
            {
                token = newJwtToken
            }));
        }