Beispiel #1
0
        public async Task <IActionResult> PatchPlace([FromBody] PlaceUpdateModel model, [FromRoute][Required] int id)
        {
            var command = Mapper.Map <App.Commands.Place.Update.CommandUpdatePlace>(model);

            command.Id = id;

            var place = await Mediator.Send(command);

            return(NoContent());
        }
        public async Task <object> Update(PlaceUpdateModel plc)
        {
            #region CheckModelIsInvalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            #endregion

            Place place = await _context.Places.FindAsync(plc.Place.Id);

            List <string> RelationIds = new List <string>();

            #region CheckIsNull
            if (place == null)
            {
                return(NotFound());
            }
            #endregion

            place.Address         = plc.Place.Address;
            place.CategoryId      = plc.Place.CategoryId;
            place.CityId          = plc.Place.CityId;
            place.Desc            = plc.Place.Desc;
            place.Gender          = plc.Place.Gender;
            place.HideContactInfo = plc.Place.HideContactInfo;
            place.HideMap         = plc.Place.HideMap;
            place.Lat             = plc.Place.Lat;
            place.Long            = plc.Place.Long;
            place.Name            = plc.Place.Name;
            place.Phone           = plc.Place.Phone;
            place.ShortDesc       = plc.Place.ShortDesc;
            place.Website         = plc.Place.Website;
            place.Video           = plc.Place.Video;
            place.Price           = plc.Place.Price;

            /*File Upload*/
            try
            {
                /*Check photo is deleted*/
                if (string.IsNullOrWhiteSpace(plc.Place.PhotoFileName))
                {
                    place.PhotoFileName = null;
                    place.Photo         = null;
                }
                /*Check photo is updated*/
                else if (!string.IsNullOrWhiteSpace(plc.Place.Photo))
                {
                    place.PhotoFileName = plc.Place.PhotoFileName;
                    place.Photo         = FileManager.Upload(plc.Place.Photo, plc.Place.PhotoFileName);
                }
            }
            catch (Exception)
            {
            }

            /*Place Socials Update Start*/
            RelationIds = new List <string>();
            if (plc.Socials != null && plc.Socials.Count > 0)
            {
                foreach (var social in plc.Socials)
                {
                    Social scl = new Social();
                    /*Create new*/
                    if (string.IsNullOrWhiteSpace(social.Id))
                    {
                        #region CheckIsEmpty
                        if (string.IsNullOrWhiteSpace(social.Name) || string.IsNullOrWhiteSpace(social.Link))
                        {
                            continue;
                        }
                        #endregion

                        #region CheckSocialExist
                        if (await _context.Socials.AnyAsync(s => s.Name == social.Name && s.Link == s.Link && s.PlaceId == place.Id))
                        {
                            RelationIds.Add(_context.Socials.FirstOrDefault(s => s.Name == social.Name && s.Link == s.Link && s.PlaceId == place.Id).Id);
                            continue;
                        }
                        #endregion

                        scl.Name    = social.Name;
                        scl.Link    = social.Link;
                        scl.PlaceId = place.Id;
                        scl.Type    = SocialType.Place;

                        /*Save Changes*/
                        try
                        {
                            await _context.Socials.AddAsync(scl);

                            await _context.SaveChangesAsync();

                            RelationIds.Add(scl.Id);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    /*Edit*/
                    else
                    {
                        scl = await _context.Socials.FirstOrDefaultAsync(s => s.Id == social.Id && s.PlaceId == place.Id);

                        #region CheckIsNull
                        if (scl == null)
                        {
                            continue;
                        }
                        #endregion

                        RelationIds.Add(scl.Id);

                        #region CheckSocialExist
                        if (await _context.Socials.AnyAsync(s => s.Name == social.Name && s.Link == s.Link && s.PlaceId == place.Id && s.Id != social.Id))
                        {
                            continue;
                        }
                        #endregion

                        #region CheckIsEmpty
                        if (string.IsNullOrWhiteSpace(social.Name) || string.IsNullOrWhiteSpace(social.Link))
                        {
                            continue;
                        }
                        #endregion

                        scl.Name = social.Name;
                        scl.Link = social.Link;

                        /*Save Changes*/
                        try
                        {
                            await _context.SaveChangesAsync();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
            /*Remove socials*/
            try
            {
                _context.Socials.RemoveRange(_context.Socials.Where(s => s.PlaceId == place.Id && !RelationIds.Contains(s.Id)));
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            /*Place Socials Update End*/

            /*Place Faqs Update Start*/
            RelationIds = new List <string>();
            if (plc.Faqs != null && plc.Faqs.Count > 0)
            {
                foreach (var faq in plc.Faqs)
                {
                    PlaceFaq placeFaq = new PlaceFaq();
                    /*Create new*/
                    if (string.IsNullOrWhiteSpace(faq.Id))
                    {
                        #region CheckIsEmpty
                        if (string.IsNullOrWhiteSpace(faq.Question) || string.IsNullOrWhiteSpace(faq.Answer))
                        {
                            continue;
                        }
                        #endregion

                        #region CheckFaqExist
                        if (await _context.PlaceFaqs.AnyAsync(f => f.Answer == faq.Answer && f.Question == faq.Question && f.PlaceId == place.Id))
                        {
                            RelationIds.Add(_context.PlaceFaqs.FirstOrDefault(f => f.Answer == faq.Answer && f.Question == faq.Question && f.PlaceId == place.Id).Id);
                            continue;
                        }
                        #endregion

                        placeFaq.Question = faq.Question;
                        placeFaq.Answer   = faq.Answer;
                        placeFaq.PlaceId  = place.Id;

                        /*Save Changes*/
                        try
                        {
                            await _context.PlaceFaqs.AddAsync(placeFaq);

                            await _context.SaveChangesAsync();

                            RelationIds.Add(placeFaq.Id);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    /*Edit*/
                    else
                    {
                        placeFaq = await _context.PlaceFaqs.FirstOrDefaultAsync(f => f.Id == faq.Id && f.PlaceId == place.Id);

                        #region CheckIsNull
                        if (placeFaq == null)
                        {
                            continue;
                        }
                        #endregion

                        RelationIds.Add(faq.Id);

                        #region CheckIsEmpty
                        if (string.IsNullOrWhiteSpace(faq.Question) || string.IsNullOrWhiteSpace(faq.Answer))
                        {
                            continue;
                        }
                        #endregion

                        #region CheckFaqExist
                        if (await _context.PlaceFaqs.AnyAsync(f => f.Id != faq.Id && f.PlaceId == place.Id && f.Answer == faq.Answer && f.Question == faq.Question))
                        {
                            continue;
                        }
                        #endregion

                        placeFaq.Question = faq.Question;
                        placeFaq.Answer   = faq.Answer;

                        /*Save Changes*/
                        try
                        {
                            await _context.SaveChangesAsync();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
            /*Remove faqs*/
            try
            {
                _context.PlaceFaqs.RemoveRange(_context.PlaceFaqs.Where(f => f.PlaceId == place.Id && !RelationIds.Contains(f.Id)));
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            /*Place Faqs Update End*/

            /*Place Slider Update Start*/
            RelationIds = new List <string>();
            if (plc.SliderPhotos != null && plc.SliderPhotos.Count > 0)
            {
                foreach (var slider in plc.SliderPhotos)
                {
                    PlaceSliderPhotos photo = new PlaceSliderPhotos();
                    if (string.IsNullOrWhiteSpace(slider.Id))
                    {
                        #region CheckIsNull
                        if (string.IsNullOrWhiteSpace(slider.Photo) || string.IsNullOrWhiteSpace(slider.PhotoName))
                        {
                            continue;
                        }
                        #endregion

                        try
                        {
                            photo.Photo     = FileManager.Upload(slider.Photo, slider.PhotoName);
                            photo.PhotoName = slider.PhotoName;
                            photo.PlaceId   = place.Id;

                            await _context.PlaceSliderPhotos.AddAsync(photo);

                            await _context.SaveChangesAsync();

                            RelationIds.Add(photo.Id);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        photo = await _context.PlaceSliderPhotos.FirstOrDefaultAsync(s => s.Id == slider.Id && s.PlaceId == place.Id);

                        #region CheckIsNull
                        if (photo == null)
                        {
                            continue;
                        }
                        #endregion

                        RelationIds.Add(photo.Id);
                    }
                }
            }
            /*Remove sliders*/
            try
            {
                _context.PlaceSliderPhotos.RemoveRange(_context.PlaceSliderPhotos.Where(s => s.PlaceId == place.Id && !RelationIds.Contains(s.Id)));
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
            }
            /*Place Slider Update End*/

            /*Place Tags Update Start*/
            RelationIds = new List <string>();
            if (plc.Tags != null && plc.Tags.Count > 0)
            {
                foreach (var tag in plc.Tags)
                {
                    #region CheckIsNull
                    if (!await _context.Tags.AnyAsync(t => t.Id == tag.Id))
                    {
                        continue;
                    }
                    #endregion

                    PlacesTags plcTag = await _context.PlacesTags.FirstOrDefaultAsync(pt => pt.TagId == tag.Id && pt.PlaceId == place.Id);

                    /*Create new*/
                    if (plcTag == null)
                    {
                        plcTag.PlaceId = place.Id;
                        plcTag.TagId   = tag.Id;

                        /*SaveChanges*/
                        try
                        {
                            await _context.PlacesTags.AddAsync(plcTag);

                            await _context.SaveChangesAsync();

                            RelationIds.Add(plcTag.Id);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    RelationIds.Add(plcTag.Id);
                }
            }
            /*Remove PlaceTag*/
            try
            {
                _context.PlacesTags.RemoveRange(_context.PlacesTags.Where(t => t.PlaceId == place.Id && !RelationIds.Contains(t.Id)));
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
            }
            /*Place Tags Update End*/

            /*Place Work Hours Update Start*/
            RelationIds = new List <string>();
            if (plc.WorkHours != null && plc.WorkHours.Count > 0)
            {
                foreach (var hour in plc.WorkHours)
                {
                    WorkHour workHour = new WorkHour();
                    /*Create new*/
                    if (string.IsNullOrWhiteSpace(hour.Id))
                    {
                        #region CheckIsEmpty
                        if (hour.Open == null || hour.Close == null)
                        {
                            continue;
                        }
                        #endregion

                        #region CheckWorkHourExist
                        if (await _context.WorkHours.AnyAsync(wh => wh.PlaceId == place.Id && wh.Day == hour.Day))
                        {
                            RelationIds.Add(_context.WorkHours.FirstOrDefault(wh => wh.PlaceId == place.Id && wh.Day == hour.Day).Id);
                            continue;
                        }
                        #endregion

                        workHour.Day     = hour.Day;
                        workHour.Open    = hour.Open;
                        workHour.Close   = hour.Close;
                        workHour.PlaceId = place.Id;

                        /*Save Changes*/
                        try
                        {
                            await _context.WorkHours.AddAsync(workHour);

                            await _context.SaveChangesAsync();

                            RelationIds.Add(workHour.Id);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    /*Edit*/
                    else
                    {
                        workHour = await _context.WorkHours.FirstOrDefaultAsync(wh => wh.PlaceId == place.Id && wh.Id == hour.Id);

                        #region CheckIsNull
                        if (workHour == null)
                        {
                            continue;
                        }
                        #endregion

                        RelationIds.Add(workHour.Id);

                        #region CheckIsEmpty
                        if (hour.Open == null || hour.Close == null)
                        {
                            continue;
                        }
                        #endregion

                        #region CheckWorkHourExist
                        if (await _context.WorkHours.AnyAsync(wh => wh.Id != workHour.Id && wh.PlaceId == place.Id && wh.Day == hour.Day))
                        {
                            continue;
                        }
                        #endregion

                        workHour.Open  = hour.Open;
                        workHour.Close = hour.Close;

                        /*Save Changes*/
                        try
                        {
                            await _context.SaveChangesAsync();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
            /*Remove WorkHours*/
            try
            {
                _context.WorkHours.RemoveRange(_context.WorkHours.Where(wh => wh.PlaceId == place.Id && !RelationIds.Contains(wh.Id)));
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
            }
            /*Place Faqs Update End*/

            return(NoContent());
        }