Exemple #1
0
        public async Task <IActionResult> CreateUpdate(PharmacyCreateUpdateViewModel model)
        {
            try
            {
                if (model.Id != null)
                {
                    var pharmacy = await _pharmacyRepository.GetByIdAsync(model.Id.Value);

                    if (pharmacy == null)
                    {
                        throw new Exception("Pharmacy Not Found");
                    }

                    pharmacy.Name           = model.Name;
                    pharmacy.Name_Ku        = model.Name_Ku;
                    pharmacy.Name_Ar        = model.Name_Ar;
                    pharmacy.Address        = model.Address;
                    pharmacy.Address_Ku     = model.Address_Ku;
                    pharmacy.Address_Ar     = model.Address_Ar;
                    pharmacy.Description    = model.Description;
                    pharmacy.Description_Ku = model.Description_Ku;
                    pharmacy.Description_Ar = model.Description_Ar;
                    pharmacy.CityId         = model.CityId;
                    pharmacy.UpdatedAt      = DateTime.Now;
                    double.TryParse(model.GoogleMap_lng, out double x_longitude);
                    double.TryParse(model.GoogleMap_lat, out double y_latitude);
                    pharmacy.Location = new NetTopologySuite.Geometries.Point(x_longitude, y_latitude)
                    {
                        SRID = 4326
                    };
                    _pharmacyRepository.UpdatePharmacy(pharmacy);

                    return(Json(new { success = true }));
                }
                else
                {
                    double.TryParse(model.GoogleMap_lng, out double x_longitude);
                    double.TryParse(model.GoogleMap_lat, out double y_latitude);

                    var pharmacy = new Pharmacy
                    {
                        Name           = model.Name,
                        Name_Ku        = model.Name_Ku,
                        Name_Ar        = model.Name_Ar,
                        Address        = model.Address,
                        Address_Ku     = model.Address_Ku,
                        Address_Ar     = model.Address_Ar,
                        Description    = model.Description,
                        Description_Ku = model.Description_Ku,
                        Description_Ar = model.Description_Ar,
                        CityId         = model.CityId,
                        Location       = new NetTopologySuite.Geometries.Point(x_longitude, y_latitude)
                        {
                            SRID = 4326 // Set the SRID (spatial reference system id) to 4326, which is the spatial reference system used by Google maps (WGS84)
                        }
                    };

                    await _pharmacyRepository.InsertPharmacyAsync(pharmacy);

                    return(Json(new { success = true }));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }