Exemple #1
0
        public async Task <IActionResult> PutUserInvestments([FromRoute] int id, [FromBody] UserInvestments userInvestments)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userInvestments.userInvestmentsId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Exemple #2
0
        public async Task <IActionResult> PostUserInvestments([FromBody] UserInvestments userInvestments)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userInvestmentsRecord = await _context.UserInvestments
                                        .FirstOrDefaultAsync(i => i.userID == userInvestments.userID && i.projectID == userInvestments.projectID);

            var proj = await _context.Projects
                       .FirstOrDefaultAsync(i => i.projectId == userInvestments.projectID);

            if (userInvestmentsRecord != null)
            {
                return(this.StatusCode(405, "You already invested in this project!"));
            }
            else if (proj.remainingFund < userInvestments.investmentAmount)
            {
                return(this.StatusCode(405, "The invested amount is more than remaining amount!"));
            }



            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.UserInvestments.Add(userInvestments);
                    await _context.SaveChangesAsync();

                    proj.remainingFund = proj.remainingFund - userInvestments.investmentAmount;

                    _context.Projects.Attach(proj);
                    _context.Entry(proj).Property(x => x.remainingFund).IsModified = true;
                    _context.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
            }

            return(CreatedAtAction("GetUserInvestments", new { id = userInvestments.userInvestmentsId }, userInvestments));
        }