Example #1
0
        public async Task <IActionResult> PutPerson([FromRoute] int id, [FromBody] EditPersonDto editPersonDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != editPersonDto.Id)
            {
                return(BadRequest());
            }

            var prePerson = _mapper.Map <Person>(editPersonDto);

            _repo.Update(prePerson);
            await _repo.SaveAsync(prePerson);

            return(NoContent());
        }
        public IActionResult Edit(int id, EditPersonDto model)
        {
            this.repo.Edit(model);

            return(StatusCode(201));
        }
Example #3
0
        //Edit concrete person
        public void Edit(EditPersonDto model)
        {
            string sql = @"UPDATE Person
            SET FirstName = @FirstName, LastName = @LastName, EGN = @EGN, Height = @Height, Weight = @Weight
            WHERE Id = @Id;";

            var visitedCountries = model.Countries;

            if (visitedCountries != null)
            {
                // Clean up our strings from ' " '
                for (int i = 0; i < visitedCountries.Length; i++)
                {
                    visitedCountries[i] = visitedCountries[i].Replace("\"", string.Empty);
                }
            }

            // Get all the countries all from DB
            IEnumerable <Country> countries = null;

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Execute(sql, new {
                    FirstName = model.FirstName, LastName = model.LastName,
                    EGN       = model.EGN, Height = model.Height, Weight = model.Weight, Id = model.Id
                });

                countries = connection.Query <Country>("SELECT Id, CountryName FROM Countries");
            }

            // Get the ids of the countries that the user marked as visited
            IList <int> countriesId = new List <int>();


            if (visitedCountries != null)
            {
                for (int i = 0; i < visitedCountries.Length; i++)
                {
                    countriesId.Add(countries.FirstOrDefault(c => c.CountryName == visitedCountries[i]).Id);
                }
            }


            // If there are no visited countries we break the method
            // But in case that the user just unchecked all the visited countries we execute this query
            if (countriesId.Count() == 0)
            {
                string sqlDelete = "DELETE FROM PersonsCountries WHERE Id_Person =@Id_Person;";
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Execute(sqlDelete, new { Id_Person = model.Id });
                }

                return;
            }

            // If there are marked visited countries we create a string builder to create our query

            StringBuilder sqlUpdateJunctionTable = new StringBuilder();

            sqlUpdateJunctionTable.Append(@"DELETE FROM PersonsCountries WHERE Id_Person =@Id_Person;
                INSERT INTO PersonsCountries (Id_Person, Id_Country) VALUES ");

            for (int i = 0; i < countriesId.Count(); i++)
            {
                sqlUpdateJunctionTable.Append($"(@Id_Person, {countriesId[i]})");
                if (i != countriesId.Count() - 1)
                {
                    sqlUpdateJunctionTable.Append(",");
                }
            }

            string sqlUpdate = sqlUpdateJunctionTable.ToString();

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Execute(sqlUpdate, new { Id_Person = model.Id });
            }
        }