Ejemplo n.º 1
0
        public IHttpActionResult PutAccount(int id, Account account)
        {
            if (!ModelState.IsValid || (account == null))
            {
                return(BadRequest(ModelState));
            }

            if (id != account.ID)
            {
                return(BadRequest());
            }

            db.Entry(account).State = EntityState.Modified;

            try
            {
                account.DateUpdated = DateTime.Now;
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccountExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public IHttpActionResult PutTrade(int id, Trade trade)
        {
            if (!ModelState.IsValid || (trade == null))
            {
                return(BadRequest(ModelState));
            }

            if (id != trade.ID)
            {
                return(BadRequest());
            }

            Trade CurrentTrade = db.Trades.AsNoTracking().Where(t => t.ID == id).FirstOrDefault();

            if (CurrentTrade == null)
            {
                return(NotFound());
            }

            // Ensure AccountId does not change
            trade.AccountId = CurrentTrade.AccountId;

            // Ensure dates are set
            DateTime dtNow = DateTime.Now;

            trade.DateUpdated = dtNow;

            // This needs to be handled better if the client doesn't send in a full PUT like they're supposed to
            if (trade.DateCreated == DateTime.MinValue)
            {
                trade.DateCreated = dtNow;
            }


            db.Entry(trade).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TradeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, ex.Message));
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 3
0
        public async Task <User> Update(User user)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Users.FindAsync(user.Id);

                if (result == null)
                {
                    return(null);
                }
                context.Entry(result).CurrentValues.SetValues(user);
                context.Entry(result).State = EntityState.Modified;
                var updated = await context.SaveChangesAsync();

                if (updated == 0)
                {
                    return(null);
                }
                return(user);
            }
        }
Ejemplo n.º 4
0
        public async Task <Avatar> Update(int userId, Avatar avatar)
        {
            using (var context = new TradePMREntities())
            {
                var user = await context.Users.FindAsync(userId);

                if (user == null)
                {
                    throw new ApplicationException($"User with id {userId} not found");
                }

                if (user.AvatarId == null)
                {
                    avatar.Users.Add(user);
                    context.Avatars.Add(avatar);
                }
                else
                {
                    var result = await context.Avatars.FindAsync(user.AvatarId);

                    if (result == null)
                    {
                        return(null);
                    }
                    avatar.Id = result.Id;
                    context.Entry(result).CurrentValues.SetValues(avatar);
                    context.Entry(result).State = EntityState.Modified;
                }

                var updated = await context.SaveChangesAsync();

                if (updated == 0)
                {
                    return(null);
                }
                return(avatar);
            }
        }
Ejemplo n.º 5
0
        public async Task <bool> Delete(User user)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Users.FindAsync(user.Id);

                if (result == null)
                {
                    return(false);
                }
                if (result.Avatar != null)
                {
                    context.Entry(result.Avatar).State = EntityState.Deleted;
                }
                context.Entry(result).State = EntityState.Deleted;
                var deleted = await context.SaveChangesAsync();

                if (deleted == 0)
                {
                    return(false);
                }
                return(true);
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> Delete(int avatarId)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Avatars.FindAsync(avatarId);

                if (result == null)
                {
                    return(false);
                }
                foreach (var user in result.Users)
                {
                    context.Entry(user).Entity.AvatarId = null;
                }
                context.Entry(result).State = EntityState.Deleted;
                var deleted = await context.SaveChangesAsync();

                if (deleted == 0)
                {
                    return(false);
                }
                return(true);
            }
        }