Beispiel #1
0
        public async Task <IActionResult> Edit(int id, EstablishmentEntity establishmentEntity)
        {
            if (id != establishmentEntity.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(establishmentEntity);
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.Message.Contains("Duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "Already exits a Establishment");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.InnerException.Message);
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(establishmentEntity));
        }
Beispiel #2
0
 public EstablishmentResponse ToEstablishmentResponse(EstablishmentEntity establishmentEntity)
 {
     return(new EstablishmentResponse
     {
         Id = establishmentEntity.Id,
         Name = establishmentEntity.Name,
         LogoEstablishmentPath = establishmentEntity.LogoEstablishmentPath,
         EstablishmentLocations = establishmentEntity.EstablishmentLocations?.Select(
             el => new EstablishmentLocationResponse
         {
             Id = el.Id,
             SourceLatitude = el.SourceLatitude,
             SourceLongitude = el.SourceLongitude,
             TargetLatitude = el.TargetLatitude,
             TargetLongitude = el.TargetLongitude,
             Remarks = el.Remarks,
             Cities = ToCityResponse(el.Cities),
             TypeEstablishment = ToTypeEstablishmentResponse(el.TypeEstablishment),
             Foods = el.Foods?.Select(f => new FoodResponse
             {
                 Id = f.Id,
                 FoodName = f.FoodName,
                 Qualification = f.Qualification,
                 Remarks = f.Remarks,
                 PicturePathFood = f.PicturePathFood,
                 EstablishmentLocationId = el.Id.ToString(),
                 TypeFoods = ToTypeFoodResponse(f.TypeFoods),
                 User = ToUserResponse(f.User)
             }).ToList()
         }).ToList()
     });
 }
Beispiel #3
0
        public async Task <IActionResult> Create(EstablishmentEntity establishmentEntity)
        {
            if (ModelState.IsValid)
            {
                establishmentEntity.Name = establishmentEntity.Name.ToUpper();
                _context.Add(establishmentEntity);
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.Message.Contains("Duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "Already exits a Establishment");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.InnerException.Message);
                    }
                }
            }
            return(View(establishmentEntity));
        }
Beispiel #4
0
        public async Task <IActionResult> PostEstablishmentLocation([FromBody] EstablishmentLocationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Bad request",
                    Result = ModelState
                }));
            }

            EstablishmentEntity establishment = await _context.Establishments.FirstOrDefaultAsync(e => e.Id == request.IdEstablishment);

            CityEntity city = await _context.Cities.FirstOrDefaultAsync(c => c.Id == request.IdCity);

            TypeEstablishmentEntity typeEstablishment = await _context.TypeEstablishments.FirstOrDefaultAsync(te => te.Id == request.IdTypeEstablishment);

            List <EstablishmentLocationEntity> establishmentLocationEntity = await _context.EstablishmentLocations
                                                                             .Include(m => m.Establishments)
                                                                             .Include(m => m.Foods)
                                                                             .ThenInclude(m => m.TypeFoods)
                                                                             .Include(m => m.Foods)
                                                                             .ThenInclude(m => m.User)
                                                                             .Include(m => m.Cities)
                                                                             .Include(m => m.TypeEstablishment)
                                                                             .Where(e => e.Establishments.Id == request.IdEstablishment)
                                                                             .ToListAsync();

            var LatitudeValidation  = establishmentLocationEntity.FirstOrDefault(el => el.SourceLatitude == request.SourceLatitude);
            var LongitudeValidation = establishmentLocationEntity.FirstOrDefault(el => el.SourceLongitude == request.SourceLongitude);

            if (LatitudeValidation != null & LongitudeValidation != null)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Establishment location exist"
                }));
            }

            EstablishmentLocationEntity establishmentLocation = new EstablishmentLocationEntity
            {
                SourceLatitude    = request.SourceLatitude,
                SourceLongitude   = request.SourceLongitude,
                TargetLatitude    = request.TargetLatitude,
                TargetLongitude   = request.TargetLongitude,
                Remarks           = request.Remarks,
                Establishments    = establishment,
                Cities            = city,
                TypeEstablishment = typeEstablishment
            };

            _context.EstablishmentLocations.Add(establishmentLocation);
            await _context.SaveChangesAsync();

            return(Ok(_converterHelper.ToEstablishmentLocationResponse(establishmentLocation)));
        }
Beispiel #5
0
        public async Task <IActionResult> PostEstablishment([FromBody] EstablishmentRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Bad request",
                    Result = ModelState
                }));
            }

            UserEntity userEntity = await _userHelper.GetUserAsync(request.UserId);

            if (userEntity == null)
            {
                return(BadRequest("User doesn't exists."));
            }

            EstablishmentEntity establishment = await _context.Establishments.FirstOrDefaultAsync(e => e.Name == request.Name);

            if (establishment != null)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Establishment exist"
                }));
            }

            string picturePath = string.Empty;

            if (request.PictureEstablishmentArray != null && request.PictureEstablishmentArray.Length > 0)
            {
                picturePath = await _blobHelper.UploadBlobAsync(request.PictureEstablishmentArray, "establishments");
            }

            establishment = new EstablishmentEntity
            {
                Name = request.Name,
                LogoEstablishmentPath = picturePath,
                User = userEntity
            };

            _context.Establishments.Add(establishment);
            await _context.SaveChangesAsync();

            return(Ok(_converterHelper.ToEstablishmentResponse(establishment)));
        }
        public static EstablishmentModel Map(EstablishmentEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            return(new EstablishmentModel()
            {
                Id = entity.Id,
                Name = entity.Name,
                Description = entity.Description,
                Preview = ImageMapper.Map(entity.Preview),
            });
        }
Beispiel #7
0
        /*  // GET: EstablishmentEntities/Create
         * public IActionResult Create()
         *  {
         *      return View();
         *  }
         *
         *  [HttpPost]
         *  [ValidateAntiForgeryToken]
         *  public async Task<IActionResult> Create(EstablishmentEntity model)
         *  {
         *      if (ModelState.IsValid)
         *      {
         *        if (ModelState.IsValid)
         *        {
         *            string path = model.LogoEstablishmentPath;
         *
         *            if (model.PictureFile != null)
         *            {
         *                path = await _imageHelper.UploadImageAsync(model.PictureFile, "Users");
         *            }
         *
         *            Entity user = await _userHelper.GetUserByEmailAsync(User.Identity.Name);
         *
         *            user.FirstName = model.FirstName;
         *            user.LastName = model.LastName;
         *            user.PhoneNumber = model.PhoneNumber;
         *            user.PicturePath = path;
         *
         *            await _userHelper.UpdateUserAsync(user);
         *            return RedirectToAction("Index", "Home");
         *        }
         *
         *        return View(model);
         *    }
         *
         *
         * }*/

        // GET: EstablishmentEntities/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            EstablishmentEntity establishmentEntity = await _context.Establishments.FindAsync(id);

            if (establishmentEntity == null)
            {
                return(NotFound());
            }
            return(View(establishmentEntity));
        }
Beispiel #8
0
        public async Task <IActionResult> GetEstablishmentEntity([FromRoute] string name)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            EstablishmentEntity establishmentEntity = await _context.Establishments
                                                      .Include(e => e.EstablishmentLocations)
                                                      .ThenInclude(e => e.Cities)
                                                      .Include(e => e.EstablishmentLocations)
                                                      .ThenInclude(e => e.TypeEstablishment)
                                                      .FirstOrDefaultAsync(e => e.Name == name);

            if (establishmentEntity == null)
            {
            }

            return(Ok(_converterHelper.ToEstablishmentResponse(establishmentEntity)));
        }
Beispiel #9
0
        // GET: EstablishmentEntities/Delete/5
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            EstablishmentEntity establishmentEntity = await _context.Establishments
                                                      .Include(t => t.EstablishmentLocations)
                                                      .FirstOrDefaultAsync(m => m.Id == id);

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

            _context.Establishments.Remove(establishmentEntity);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #10
0
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            EstablishmentEntity establishmentEntity = await _context.Establishments
                                                      .Include(m => m.EstablishmentLocations)
                                                      .ThenInclude(m => m.Cities)
                                                      .Include(m => m.EstablishmentLocations)
                                                      .ThenInclude(m => m.Foods)
                                                      .Include(m => m.EstablishmentLocations)
                                                      .ThenInclude(m => m.TypeEstablishment)
                                                      .FirstOrDefaultAsync(m => m.Id == id);

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

            return(View(establishmentEntity));
        }