コード例 #1
0
ファイル: AdController.cs プロジェクト: RTUITLab/StuDo-Back
        public async Task <ActionResult <AdView> > CreateAdAsync([FromBody] AdCreateRequest adCreateRequest)
        {
            var currentUserId = GetCurrentUserId();

            try
            {
                var createdAd = await adManager.AddAsync(adCreateRequest, currentUserId);

                AdView newAd = await createdAd
                               .AttachCurrentUserId(mapper.ConfigurationProvider, GetCurrentUserId())
                               .ProjectTo <AdView>(mapper.ConfigurationProvider)
                               .SingleAsync();

                return(Ok(newAd));
            }
            catch (ArgumentNullException ane)
            {
                logger.LogDebug(ane.Message + "\n" + ane.StackTrace);
                if (adCreateRequest.OrganizationId.HasValue)
                {
                    logger.LogDebug($"Organization {adCreateRequest.OrganizationId.Value} doesn't exist in database");
                    return(NotFound($"Organization {adCreateRequest.OrganizationId.Value} doesn't exist in database"));
                }
                else
                {
                    logger.LogDebug($"Current user {currentUserId} doesn't exist in database");
                    return(NotFound($"Current user {currentUserId} doesn't exist in database"));
                }
            }
            catch (MethodAccessException mae)
            {
                logger.LogDebug(mae.Message + "\n" + mae.StackTrace);
                logger.LogDebug($"Current user {currentUserId} has no rights to create ads in organization {adCreateRequest.OrganizationId.Value}");
                return(Forbid(JwtBearerDefaults.AuthenticationScheme, CookieAuthenticationDefaults.AuthenticationScheme));
            }
            catch (Exception ex)
            {
                logger.LogDebug(ex.Message + "\n" + ex.StackTrace);
                return(StatusCode(500));
            }
        }