示例#1
0
        public async Task <IActionResult> PutPostModerator(int id, PostModerator postModerator)
        {
            if (id != postModerator.Id)
            {
                return(BadRequest());
            }

            _context.Entry(postModerator).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostModeratorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#2
0
        public async Task <ActionResult <PostModerator> > PostPostModerator(PostModerator postModerator)
        {
            _context.postModerators.Add(postModerator);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetPostModerator", new { id = postModerator.Id }, postModerator));
        }
        public async Task <ActionResult> PostJob(JobPost postData, string postType)
        {
            // ModelState Error
            if (!ModelState.IsValid)
            {
                var error = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var err in state.Value.Errors)
                    {
                        error.Add(err.ToString());
                    }
                }
                return(BadRequest(error));
            }
            // Get Login user Id
            var user = User.FindFirstValue(ClaimTypes.NameIdentifier);

            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    JobPost post = postData;
                    post.userId = Guid.Parse(user); // Convert string to Guid
                    // Save as Draft
                    if (postType == "draft")
                    {
                        // Enum postStatus 0==Draft
                        post.PostStatus = Enum.Parse <JobPostStatus>("Draft"); // Convert string to Enum

                        if (postData.Id == 0)
                        {
                            // Save First Time
                            await _context.jobPosts.AddAsync(post);

                            await _context.SaveChangesAsync();
                        }
                        else
                        {
                            // IF exist Update
                            _context.Entry <JobPost>(post).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                        }

                        await transaction.CommitAsync();

                        return(Created("", post));
                    }
                    // Save as Published
                    else if (postType == "Published")
                    {
                        // Enum postStatus 1==Published
                        post.PostStatus = JobPostStatus.Published;

                        // Exists or Not
                        var package = await _context.servicePackages.FindAsync(post.PackageId);

                        Invoice       checkInvoice       = _context.invoices.Where(i => i.JobPostId == post.Id).AsNoTracking().FirstOrDefault();
                        PostModerator checkPostModeretor = _context.postModerators.Where(p => p.jobPostId == post.Id).AsNoTracking().FirstOrDefault();
                        // .......

                        int invoiceId = 0;
                        if (checkInvoice != null)
                        {
                            invoiceId = checkInvoice.Id;
                        }
                        Invoice invoice = new Invoice
                        {
                            Id            = invoiceId,
                            JobPostId     = post.Id,
                            PackagesId    = package.Id,
                            Quantity      = 1,
                            UnitPrice     = package.Price,
                            PaymentStatus = false,
                            TotalAmount   = package.Price,
                            UserId        = Guid.Parse(user),
                            Date          = DateTime.Now
                        };

                        PostModerator postModerate = new PostModerator
                        {
                            UserId    = Guid.Parse(user),
                            jobPostId = post.Id,
                            Date      = DateTime.Now,
                            Status    = PostStatus.Active
                        };

                        if (post.Id == 0)
                        {
                            await _context.jobPosts.AddAsync(post);

                            await _context.SaveChangesAsync();

                            invoice.JobPostId      = post.Id;
                            postModerate.jobPostId = post.Id;

                            await _context.invoices.AddAsync(invoice);

                            await _context.SaveChangesAsync();

                            // If not exists then create
                            if (checkPostModeretor == null)
                            {
                                await _context.postModerators.AddAsync(postModerate);

                                await _context.SaveChangesAsync();
                            }


                            await transaction.CommitAsync();

                            return(Created("", post));
                        }
                        else
                        {
                            _context.Entry <JobPost>(post).State = EntityState.Modified;

                            // If not exists then create
                            if (checkPostModeretor == null)
                            {
                                await _context.postModerators.AddAsync(postModerate);

                                await _context.SaveChangesAsync();
                            }

                            await _context.SaveChangesAsync();

                            if (checkInvoice == null)
                            {
                                await _context.invoices.AddAsync(invoice);

                                await _context.SaveChangesAsync();
                            }
                            else
                            {
                                _context.Entry <Invoice>(invoice).State = EntityState.Modified;
                            }
                            await transaction.CommitAsync();

                            return(Ok());
                        }
                    }
                }
                catch (Exception ex)
                {
                    await transaction.RollbackAsync();

                    throw ex;
                }
            }
            return(BadRequest());
        }