Exemplo n.º 1
0
        public Task <HttpResponseMessage> UpdateStore([FromBody] LocationApiModel model)
        {
            return(Task <HttpResponseMessage> .Factory.StartNew(() =>
            {
                try
                {
                    if (CurrentRestaurantId == Guid.Empty)
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest, "Restaurant does not exist.");
                    }

                    if (ModelState.IsValid == false)
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest, MessagesResources.LocationInvalidData);
                    }


                    var restaurant = CurrentAppContext.Restaurants
                                     .AsNoTracking()
                                     .FirstOrDefault(r => r.Id == CurrentRestaurantId);

                    if (restaurant == null)
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest, MessagesResources.CompanyNotExist);
                    }

                    var entityLocation = CurrentAppContext.Locations
                                         .Include(l => l.Menus)
                                         .FirstOrDefault(l => l.Id == model.Id);

                    if (entityLocation == null)
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest, MessagesResources.LocationNotExist);
                    }

                    entityLocation.RestaurantId = CurrentRestaurantId;
                    entityLocation.Identifier = model.Identifier;
                    entityLocation.Description = model.Features;
                    entityLocation.Phone = model.Phone;
                    entityLocation.Streets = model.Address;
                    entityLocation.Delivery = model.Delivery;
                    entityLocation.Latitude = model.Location.Latitude;
                    entityLocation.Longitude = model.Location.Longitude;
                    entityLocation.SpatialLocation = CreatePoint(model.Location.Latitude, model.Location.Longitude);
                    entityLocation.Zone = model.Zone;

                    entityLocation.OpenDays.ToList().ForEach(od => entityLocation.OpenDays.Remove(od));
                    entityLocation.OpenDays = model.Days
                                              .Where(d => d.Open &&
                                                     string.IsNullOrEmpty(d.From) == false &&
                                                     string.IsNullOrEmpty(d.To) == false)
                                              .Select(o => new OpenDay
                    {
                        Id = Guid.NewGuid(),
                        DayOfWeek = o.DayOfWeek,
                        OpenHour = Convert.ToInt32(o.From.Split(':')[0]),
                        OpenMinutes = Convert.ToInt32(o.From.Split(':')[1]),
                        CloseHour = Convert.ToInt32(o.To.Split(':')[0]),
                        CloseMinutes = Convert.ToInt32(o.To.Split(':')[1]),
                    }).ToList();



                    foreach (var openDay in model.Days.Where(d => d.Open == false))
                    {
                        DaysApiModel day = openDay;
                        var menus =
                            entityLocation.Menus.Where(m => day.DayOfWeek == DayOfWeek.Monday ? m.MenuDays.Monday
                                : day.DayOfWeek == DayOfWeek.Tuesday ? m.MenuDays.Tuesday
                                : day.DayOfWeek == DayOfWeek.Wednesday ? m.MenuDays.Wednesday
                                : day.DayOfWeek == DayOfWeek.Thursday ? m.MenuDays.Thursday
                                : day.DayOfWeek == DayOfWeek.Friday ? m.MenuDays.Friday
                                : day.DayOfWeek == DayOfWeek.Saturday ? m.MenuDays.Saturday
                                : day.DayOfWeek == DayOfWeek.Sunday && m.MenuDays.Sunday)
                            .ToList();

                        foreach (var menu in menus.ToList())
                        {
                            entityLocation.Menus.Remove(menu);
                        }
                    }

                    CurrentAppContext.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.OK, MessagesResources.GeneralSaveSucces);
                }
                catch (Exception exception)
                {
                    ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception));
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }));
        }
Exemplo n.º 2
0
        public Task <HttpResponseMessage> Stores()
        {
            return(Task <HttpResponseMessage> .Factory.StartNew(() =>
            {
                try
                {
                    var restaurant = CurrentAppContext.Restaurants
                                     .Include(r => r.Locations.Select(o => o.OpenDays))
                                     .AsNoTracking()
                                     .FirstOrDefault(r => r.Id == CurrentRestaurantId);

                    if (restaurant == null)
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest, MessagesResources.CompanyNotExist);
                    }

                    var models = restaurant.Locations.Select(l =>
                    {
                        var model = new LocationApiModel
                        {
                            Id = l.Id,
                            Address = l.Streets,
                            Delivery = l.Delivery,
                            Features = l.Description,
                            Identifier = l.Identifier,
                            Location = new LatLong
                            {
                                Latitude = l.Latitude,
                                Longitude = l.Longitude,
                            },
                            Phone = l.Phone,
                            Zone = l.Zone,
                        };

                        #region Days

                        var monday = new DaysApiModel(DayOfWeek.Monday);
                        var odMonday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == monday.DayOfWeek);
                        if (odMonday != null)
                        {
                            monday.From = string.Format("{0}:{1}", odMonday.OpenHour.ToString("00"), odMonday.OpenMinutes.ToString("00"));
                            monday.To = string.Format("{0}:{1}", odMonday.CloseHour.ToString("00"), odMonday.CloseMinutes.ToString("00"));
                            monday.Open = true;
                        }
                        model.Days.Add(monday);

                        var tuesday = new DaysApiModel(DayOfWeek.Tuesday);
                        var odTuesday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == tuesday.DayOfWeek);
                        if (odTuesday != null)
                        {
                            tuesday.From = string.Format("{0}:{1}", odTuesday.OpenHour.ToString("00"), odTuesday.OpenMinutes.ToString("00"));
                            tuesday.To = string.Format("{0}:{1}", odTuesday.CloseHour.ToString("00"), odTuesday.CloseMinutes.ToString("00"));
                            tuesday.Open = true;
                        }
                        model.Days.Add(tuesday);

                        var wednesday = new DaysApiModel(DayOfWeek.Wednesday);
                        var odWednesday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == wednesday.DayOfWeek);
                        if (odWednesday != null)
                        {
                            wednesday.From = string.Format("{0}:{1}", odWednesday.OpenHour.ToString("00"), odWednesday.OpenMinutes.ToString("00"));
                            wednesday.To = string.Format("{0}:{1}", odWednesday.CloseHour.ToString("00"), odWednesday.CloseMinutes.ToString("00"));
                            wednesday.Open = true;
                        }
                        model.Days.Add(wednesday);

                        var thursday = new DaysApiModel(DayOfWeek.Thursday);
                        var odThursday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == thursday.DayOfWeek);
                        if (odThursday != null)
                        {
                            thursday.From = string.Format("{0}:{1}", odThursday.OpenHour.ToString("00"), odThursday.OpenMinutes.ToString("00"));
                            thursday.To = string.Format("{0}:{1}", odThursday.CloseHour.ToString("00"), odThursday.CloseMinutes.ToString("00"));
                            thursday.Open = true;
                        }
                        model.Days.Add(thursday);

                        var friday = new DaysApiModel(DayOfWeek.Friday);
                        var odFriday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == friday.DayOfWeek);
                        if (odFriday != null)
                        {
                            friday.From = string.Format("{0}:{1}", odFriday.OpenHour.ToString("00"), odFriday.OpenMinutes.ToString("00"));
                            friday.To = string.Format("{0}:{1}", odFriday.CloseHour.ToString("00"), odFriday.CloseMinutes.ToString("00"));
                            friday.Open = true;
                        }
                        model.Days.Add(friday);

                        var saturday = new DaysApiModel(DayOfWeek.Saturday);
                        var odSaturday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == saturday.DayOfWeek);
                        if (odSaturday != null)
                        {
                            saturday.From = string.Format("{0}:{1}", odSaturday.OpenHour.ToString("00"), odSaturday.OpenMinutes.ToString("00"));
                            saturday.To = string.Format("{0}:{1}", odSaturday.CloseHour.ToString("00"), odSaturday.CloseMinutes.ToString("00"));
                            saturday.Open = true;
                        }
                        model.Days.Add(saturday);

                        var sunday = new DaysApiModel(DayOfWeek.Sunday);
                        var odSunday = l.OpenDays.FirstOrDefault(od => od.DayOfWeek == sunday.DayOfWeek);
                        if (odSunday != null)
                        {
                            sunday.From = string.Format("{0}:{1}", odSunday.OpenHour.ToString("00"), odSunday.OpenMinutes.ToString("00"));
                            sunday.To = string.Format("{0}:{1}", odSunday.CloseHour.ToString("00"), odSunday.CloseMinutes.ToString("00"));
                            sunday.Open = true;
                        }
                        model.Days.Add(sunday);
                        #endregion

                        return model;
                    }).ToList();

                    return Request.CreateResponse(HttpStatusCode.OK, models);
                }
                catch (Exception exception)
                {
                    ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception));
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }));
        }