Пример #1
0
        public IHttpActionResult PutLunchProfile(int id, Models.Profile lunchProfile)
        {
            var profile = new LunchProfile()
            {
                Id            = lunchProfile.Id,
                Fav1          = lunchProfile.Fav1,
                Fav2          = lunchProfile.Fav2,
                Fav3          = lunchProfile.Fav3,
                FirstName     = lunchProfile.FirstName,
                FK_LocationId = lunchProfile.FK_LocationId,
                FK_OfficeId   = lunchProfile.FK_OfficeId,
                FK_ProviderId = lunchProfile.FK_ProviderId,
                LastName      = lunchProfile.LastName,
                Login         = lunchProfile.Login,
                LunchOrders   = lunchProfile.LunchToOrder.Select(x => new Models.EF.LunchOrder()
                {
                    DateOrdered = x.DateOrdered, FK_ProfileId = x.FK_ProfileId, Id = x.Id, Lunch = x.LunchOrder, LunchDay = x.LunchDay
                }).ToList()
            };

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LunchProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public IHttpActionResult PostLunchProfile(Models.Profile lunchProfile)
        {
            var profileExists = db.LunchProfiles.Any(x => x.Login == lunchProfile.Login);

            var profile = new LunchProfile()
            {
                Id            = lunchProfile.Id,
                Fav1          = lunchProfile.Fav1,
                Fav2          = lunchProfile.Fav2,
                Fav3          = lunchProfile.Fav3,
                FirstName     = lunchProfile.FirstName,
                FK_LocationId = lunchProfile.FK_LocationId,
                FK_OfficeId   = lunchProfile.FK_OfficeId,
                FK_ProviderId = lunchProfile.FK_ProviderId,
                LastName      = lunchProfile.LastName,
                Login         = lunchProfile.Login
                ,
                LunchOrders = lunchProfile.LunchToOrder.Select(x => new Models.EF.LunchOrder()
                {
                    DateOrdered  = x.DateOrdered,
                    FK_ProfileId = x.FK_ProfileId,
                    Id           = x.Id,
                    Lunch        = x.LunchOrder,
                    LunchDay     = x.LunchDay
                }).ToList()
            };

            var lOrder = lunchProfile.LunchToOrder.Select(x => new Models.EF.LunchOrder()
            {
                DateOrdered  = x.DateOrdered,
                FK_ProfileId = x.FK_ProfileId,
                Id           = x.Id,
                Lunch        = x.LunchOrder,
                LunchDay     = x.LunchDay
            }).ToList();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!profileExists)
            {
                db.LunchProfiles.Add(profile);
                db.SaveChanges();
                return(CreatedAtRoute("DefaultApi", new { id = lunchProfile.Id }, lunchProfile));
            }
            else
            {
                db.Entry(profile).State = EntityState.Modified;
                try
                {
                    if (profile.LunchOrders.Any() && profile.LunchOrders.First().Id != 0)
                    {
                        db.LunchOrders.Remove(profile.LunchOrders.FirstOrDefault());
                    }
                    db.LunchOrders.Add(lOrder.FirstOrDefault());
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!LunchProfileExists(lunchProfile.Login))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(StatusCode(HttpStatusCode.OK));
            }
        }