Example #1
0
        public async Task <ActionResult <Credit> > UpdateCredit(Guid id, Credit creditDto)
        {
            try
            {
                // Check if the Id entered is the same Id as the object that has been sent
                if (!(id.ToString().Equals(creditDto.CreditId.ToString())))
                {
                    return(BadRequest());
                }

                // Mappes the creditDto into an entity
                var credit = _mapper.Map <Credit>(creditDto);

                // Adds the current date as LastModified
                credit.LastModified = DateTime.Now;

                // "Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified." Source: https://docs.microsoft.com/en-us/ef/ef6/saving/change-tracking/entity-state
                _context.Entry(credit).State = EntityState.Modified;

                // Saves the changes to the database
                await _context.SaveChangesAsync();

                // Returns success and the same dto that was sent as a parameter
                return(Ok(creditDto));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CreditExist(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(StatusCode(500));
            }
        }
Example #2
0
        public async Task <IActionResult> UpdateProduct(Guid id, Product product)
        {
            try
            {
                // Check if the Id entered is the same Id as the object that has been sent
                if (!(id.ToString().Equals(product.ProductId.ToString())))
                {
                    return(BadRequest());
                }

                // "Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified." Source: https://docs.microsoft.com/en-us/ef/ef6/saving/change-tracking/entity-state
                _context.Entry(product).State = EntityState.Modified;
                try
                {
                    // Saves changes to the database
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductsExist(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(NoContent());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(StatusCode(500));
            }
        }
Example #3
0
 public Task Update(Order order)
 {
     _context.Entry(order).State = EntityState.Modified;
     return(_context.SaveChangesAsync());
 }