Example #1
0
        public async Task <IActionResult> Post([FromBody] AddEstablishmentViewModel establishmentToAdd)
        {
            if (ModelState.IsValid)
            {
                if (!IsMerchant())
                {
                    return(BadRequest(new { error = "U bent geen handelaar." }));
                }

                //modelstate werkt niet op lijsten :-D
                if (establishmentToAdd.Categories == null || !establishmentToAdd.Categories.Any())
                {
                    return(BadRequest(new { error = "Geen categorieƫn meegeven." }));
                }

                //modelstate werkt niet op lijsten :-D
                if (establishmentToAdd.Images == null || !establishmentToAdd.Images.Any())
                {
                    return(BadRequest(new { error = "Geen afbeelding(en) meegeven." }));
                }

                if (!ContainsJpgs(establishmentToAdd.Images))
                {
                    return(BadRequest(new { error = "Geen jpg afbeelding(en) meegeven." }));
                }

                if (!_companyRepository.isOwnerOfCompany(int.Parse(User.FindFirst("userId")?.Value), establishmentToAdd.CompanyId ?? 0))
                {
                    return(BadRequest(new { error = "Het bedrijf waaraan u deze vestiging wilt toevoegen behoord niet tot u." }));
                }

                // Ophalen van Latitude en Longitude op basis van het meegegeven adres
                var adress = $"{establishmentToAdd.Street}+{establishmentToAdd.HouseNumber},+{establishmentToAdd.PostalCode}+{establishmentToAdd.City},+Belgiƫ";

                List <double> latAndLong = await GetLatAndLongFromAddressAsync(adress);

                Establishment newEstablishment = new Establishment
                {
                    Name        = establishmentToAdd.Name,
                    Description = establishmentToAdd.Description,

                    Street      = establishmentToAdd.Street,
                    HouseNumber = establishmentToAdd.HouseNumber,
                    PostalCode  = establishmentToAdd.PostalCode,
                    City        = establishmentToAdd.City,
                    Latitude    = latAndLong[0],
                    Longitude   = latAndLong[1],

                    EstablishmentCategories = ConvertCategoryViewModelsToCategory(establishmentToAdd.Categories),

                    OpenDays        = ConvertOpenDaysViewModelsToOpenDays(establishmentToAdd.OpenDays),
                    ExceptionalDays = ConvertExceptionalDaysViewModelsToExceptionalDays(establishmentToAdd.ExceptionalDays),

                    EstablishmentSocialMedias = ConvertEstablishmentSocialMediasViewModelsToEstablishmentSocialMedias(establishmentToAdd.SocialMedias)
                };

                _establishmentRepository.addEstablishment(establishmentToAdd.CompanyId ?? 0, newEstablishment);

                //we hebben id nodig voor img path dus erna
                newEstablishment.Images = ConvertFileViewModelToImages(establishmentToAdd.Images, newEstablishment.EstablishmentId);
                _establishmentRepository.SaveChanges();
                return(Ok(new { bericht = "De vestiging werd succesvol toegevoegd." }));
            }
            //Als we hier zijn is is modelstate niet voldaan dus stuur error 400, slechte aanvraag
            string errorMsg = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));

            return(BadRequest(new { error = errorMsg }));
        }