public async Task <IActionResult> PutTblPosts(int id, TblPosts tblPosts)
        {
            if (id != tblPosts.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <TransactionHistory> > createTransactionHistory(TransactionHistoryDTO dto)
        {
            TransactionHistory transactionHistory = new TransactionHistory(); //truyền xuống dto có 4 field: postid, giver, receiver, transactiondate

            transactionHistory.PostId          = dto.PostId;
            transactionHistory.Giver           = dto.Giver;
            transactionHistory.Receiver        = dto.Receiver;
            transactionHistory.TransactionDate = DateTime.Now;
            TblUsersHavingPosts usersHavingPosts = _context.TblUsersHavingPosts.FromSqlRaw("select * from TblUsersHavingPosts where " +
                                                                                           "Username = {0} and PostId = {1}", dto.Receiver, dto.PostId).First(); //tìm bài post của freelancer đã hoàn thành

            usersHavingPosts.Status = "finished";                                                                                                                //set status = finished
            _context.Entry(usersHavingPosts).State = EntityState.Modified;
            TblPosts post = _context.TblPosts.FromSqlRaw("select * from TblPosts where " +
                                                         "Id = {0}", dto.PostId).First(); //tìm bài post trong TblPosts

            post.IsPublic = false;                                                        //ko public bài post nữa
            _context.Entry(post).State = EntityState.Modified;
            Int64 postAmount = _context.TblPosts.Find(dto.PostId).Amount;                 //lấy ra amount của bài post

            transactionHistory.Amount = postAmount;                                       //lưu vào transaction history
            _context.TransactionHistory.Add(transactionHistory);                          //add transaction dto vào table TransactionHistory
            TblUsers company = _context.TblUsers.Find(dto.Giver);                         //tìm ra company

            company.Amount -= postAmount;                                                 //lấy amount hiện tại của company - amount của bài post đã finished
            _context.Entry(company).State = EntityState.Modified;
            TblUsers freelancer = _context.TblUsers.Find(dto.Receiver);                   //tìm ra freelancer

            freelancer.Amount += postAmount;                                              //lấy amount hiện tại của freelancer + amount của bài post đã finished
            _context.Entry(freelancer).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(transactionHistory);
        }
        public async Task <ActionResult <TblUsers> > setStatus(UsernameDTO usernameDTO)
        {
            TblUsers userEntity = _context.TblUsers
                                  .FromSqlRaw("select * from tblUsers where Username = {0}", usernameDTO.Username).First();

            if (userEntity != null)
            {
                if (userEntity.Status.Equals("active"))
                {
                    userEntity.Status = "banned";
                    _context.Entry(userEntity).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
                else if (userEntity.Status.Equals("banned"))
                {
                    userEntity.Status = "active";
                    _context.Entry(userEntity).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
            }
            else
            {
                return(NotFound());
            }
            return(userEntity);
        }
        public async Task <ActionResult <TblUsersHavingPosts> > AddRequestedPost(RequestedPost requestedPost)
        {
            TblUsersHavingPosts tblUsersHavingPosts = new TblUsersHavingPosts();

            tblUsersHavingPosts.Username = requestedPost.Username;
            tblUsersHavingPosts.PostId   = requestedPost.PostId;
            tblUsersHavingPosts.Status   = requestedPost.Status;
            List <TblUsersHavingPosts> listAccepted = _context.TblUsersHavingPosts
                                                      .FromSqlRaw("select * from TblUsersHavingPosts where Username = {0} and Status = 'accepted'", tblUsersHavingPosts.Username)
                                                      .ToList <TblUsersHavingPosts>();

            if (listAccepted.Count > 0)
            {
                return(BadRequest());
            }
            else
            {
                _context.TblUsersHavingPosts.Add(tblUsersHavingPosts);
            }
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTblUsersHavingPosts", new { id = tblUsersHavingPosts.Id }, tblUsersHavingPosts));
        }