Exemplo n.º 1
0
        public async Task <IActionResult> CreateAsync([FromBody] PostCreateViewModel postCreateModel)
        {
            if (postCreateModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            List <FileInfoDTO> filesDTO = new List <FileInfoDTO>();

            postCreateModel.VideoUrl = string.IsNullOrEmpty(postCreateModel.VideoUrl) ? null : postCreateModel.VideoUrl;

            var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);

            postCreateModel.StartDate = postCreateModel.StartDate.AddHours(offset.TotalHours);
            postCreateModel.EndDate   = postCreateModel.EndDate.AddHours(offset.TotalHours);

            foreach (var file in postCreateModel.Files)
            {
                var createRequestFile = FileMapper.ConvertToCreateFileRequest(file);

                using (var stream = _fileManager.GetFileStream(file))
                {
                    var createdPostFileDTO = await _fileService.CreateAsync(createRequestFile, stream);

                    filesDTO.Add(createdPostFileDTO);
                }
            }

            var post = PostMapper.ConvertToPostDTO(postCreateModel);

            post.Files = filesDTO;
            var createdPostDTO = await _postService.CreateAsync(post);

            var currentUser   = _userContext.CurrentUser;
            var postViewModel = PostMapper.ConvertToPostViewModel(createdPostDTO);

            if (currentUser.RoleId != (int)Roles.Moderator)
            {
                _emailService.PostCreatedNotificationAsync(postViewModel);
            }
            return(Ok(createdPostDTO));
        }
Exemplo n.º 2
0
 private List <FileInfoDTO> MapFilesToDto(IEnumerable <FileInfoViewModel> files)
 {
     return(files
            .Select(file =>
     {
         if (file.isAttached)
         {
             var newFile = FileMapper.ConvertToCreateFileRequest(file);
             return _fileService.CreateAsync(newFile, _fileManager.GetFileStream(file)).Result;
         }
         else
         {
             return FileMapper.ConvertToFileInfoDTO(file);
         }
     })
            .ToList());
 }