async public Task <IActionResult> createCompanyPost(CreateCompanyPostDto companyPostDto)
        {
            ServiceResponse <List <GetCompanyPostAdminDto> > response = await _companyPostService.CreateCompanyPost(companyPostDto);

            if (response.Success)
            {
                return(Ok(response.Data));
            }
            else
            {
                return(NotFound(response.Message));
            }
        }
Exemple #2
0
        async Task <ServiceResponse <List <GetCompanyPostAdminDto> > > ICompanyPostService.CreateCompanyPost(CreateCompanyPostDto companyPostDto)
        {
            ServiceResponse <List <GetCompanyPostAdminDto> > response = new ServiceResponse <List <GetCompanyPostAdminDto> >();
            Company companyOfPost = await _context.Companies.FirstOrDefaultAsync(a => a.companyId == companyPostDto.companyId);

            CompanyUser companyUserOfPost = await _context.CompanyUsers.FirstOrDefaultAsync(a => a.companyUserId == companyPostDto.companyUserId);

            if (companyOfPost == null)
            {
                response.Success = false;
                response.Message = "The company for this post does not exist";
                return(response);
            }
            if (companyUserOfPost == null)
            {
                response.Success = false;
                response.Message = "The poster of the post does not exist";
                return(response);
            }


            string      finalString = new IDGenerator.IDGenerator().generate();
            string      lastUpdated = DateTime.Now.ToString();
            CompanyPost newPost     = new CompanyPost(finalString, companyPostDto.companyId, companyPostDto.companyUserId, companyOfPost.companyName, companyPostDto.postTitle, companyPostDto.postSubTitle, companyPostDto.postDescription, companyPostDto.videoUrl, companyPostDto.links, lastUpdated, companyPostDto.approvedBy, companyPostDto.validTill, true);
            await _context.CompanyPosts.AddAsync(newPost);

            await _context.SaveChangesAsync();

            List <GetCompanyPostAdminDto> postList = await _context.CompanyPosts.Select(a => new GetCompanyPostAdminDto(a.companyPostId, a.companyUserId, a.companyId, a.companyName, a.postTitle, a.postSubTitle, a.postDescription, a.videoUrl, a.links, a.lastUpdated, a.approvedBy, a.validTill, a.isActive)).ToListAsync();

            response.Data = postList;
            return(response);
        }