mercredi 21 août 2019

How can i return warning message from .NET CORE API

I am using Angular on client-side. I am using .NET CORE WEB API on server-side and i'm using SQL Server for database. My WEB API has layers. Business Logic Layer does database dml operations for me and i was able to do get and post http client operations. But i have to check data in Business Logic Layer if data is wrong formatted i want to show an error on client like "This phone number is wrong formatted. Please enter correct format". I did DML (INSERT,SELECT) operations correctly. I need help for returning error message and i want to show this error message to users.

This is .NET Core Web API PersonController.cs

    [Route("api/[controller]")]
    [ApiController]
    [EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
    public class PersonController : ControllerBase
    {
        FihristBLL bll = new FihristBLL();

        // GET: api/Person
        [HttpGet]
        public List<Person> Get()
        {
            return bll.GetNumbers();
        }


        // POST: api/Person
        [HttpPost]
        public void Post([FromBody] Person person)
        {
            bll.AddNumber(person);
        }
    }
}

This is Business Logic Layer doing database operations

    public class FihristBLL
    {
        public void AddNumber(Person person)
        {


                if (person.PhoneNumber.Substring(0, 1) != "5" || 0 % Convert.ToInt64(person.PhoneNumber) != 0)
                {
                // WRONG FORMAT


                }

                else
                {
                    // CORRECT FORMAT      
                    FihristDAL db = new FihristDAL();
                    SqlConnection conn = db.OpenConnection();
                    string command = "INSERT INTO Persons (PhoneNumber,FirstName,LastName) VALUES (@phoneNumber,@firstName,@lastName)";
                    SqlCommand sqlCommand = db.CreateConnection(command);
                    sqlCommand.Parameters.AddWithValue("@phoneNumber", person.PhoneNumber);
                    sqlCommand.Parameters.AddWithValue("@firstName", person.FirstName);
                    sqlCommand.Parameters.AddWithValue("@lastName", person.LastName);
                    sqlCommand.ExecuteNonQuery();
                    conn.Close();


                }     
        }

        public List<Person> GetNumbers()
        {
            List<Person> persons = new List<Person>();
            FihristDAL db = new FihristDAL();
            SqlConnection conn = db.OpenConnection();
            string command = "SELECT * FROM Persons";
            SqlCommand sqlCommand = db.CreateConnection(command);
            SqlDataReader dr = sqlCommand.ExecuteReader();


            while (dr.Read())
            {
                persons.Add(new Person() { PhoneNumber = dr.GetString(1), FirstName = dr.GetString(2), LastName = dr.GetString(3) });
            }

            dr.Close();
            conn.Close();

            return persons;



        }
    }
}

Operations perfectly work fine. But i need to return error messages like "You just entered wrong formatted phone number." and display this message on Angular.




Aucun commentaire:

Enregistrer un commentaire