示例#1
0
        public async Task <IHttpActionResult> PostUserValuta(UserValutaJson userValuta)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            User       user          = db.User.Find(userValuta.UserId);
            Valuta     valuta        = db.Valuta.Find(userValuta.ValutaId);
            UserValuta newUserValuta = new UserValuta {
                Amount = userValuta.Amount, PurchasePrice = userValuta.PurchasePrice, Valuta = valuta
            };

            user.UserValutas.Add(newUserValuta);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = newUserValuta.Id }, newUserValuta));
        }
示例#2
0
        public async Task <IHttpActionResult> PutUserValuta(int id, UserValutaJson userValuta)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userValuta.Id)
            {
                return(BadRequest());
            }

            User       user             = db.User.SingleOrDefault(u => u.Id == userValuta.UserId);
            UserValuta userValutaOfUser = user.UserValutas.Where(uv => uv.Id == userValuta.Id).FirstOrDefault();

            if (userValutaOfUser.Amount != userValuta.Amount)
            {
                userValutaOfUser.Amount = userValuta.Amount;
            }

            if (userValutaOfUser.PurchasePrice != userValuta.PurchasePrice)
            {
                userValutaOfUser.PurchasePrice = userValuta.PurchasePrice;
            }

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserValutaExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }