public ActionResult Post([FromBody] hireRequest req) { using (SqlConnection con = new SqlConnection(_connectionString)) { List <string> errors = new List <string>(); con.Open(); var hireValidator = new HireValidation(); var result = hireValidator.Validate(req); if (result.IsValid) { SqlCommand command = new SqlCommand("insert into hire values(@BookId,@UserId,@StartDate,@EndDate,@DeliveryDate)", con); command.Parameters.Add("@BookId", SqlDbType.Int).Value = req.bookid; command.Parameters.Add("@UserId", SqlDbType.Int).Value = req.userid; command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = req.startdate; command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = req.enddate; command.Parameters.Add("@DeliveryDate", SqlDbType.DateTime).Value = req.deliverydate; try { SqlDataReader reader = command.ExecuteReader(); return(Ok("Posted !")); } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } foreach (var i in result.Errors) { errors.Add($"ERROR! {i.PropertyName} : {i.ErrorMessage}"); } return(BadRequest(errors)); } }
public ActionResult Put([FromBody] hireRequest req, [FromRoute] int hireid) { using (SqlConnection con = new SqlConnection(_connectionString)) { con.Open(); List <string> errors = new List <string>(); var hireValidator = new HireValidation(); var result = hireValidator.Validate(req); if (result.IsValid) { SqlCommand command = new SqlCommand("update Hire set BookId=@bookid,UserId=@userid,StartDate=@startdate,Enddate=@enddate,DeliveryDate=@deliverydate where Hire.Id=" + hireid, con); command.Parameters.Add("@bookid", SqlDbType.Int).Value = req.bookid; command.Parameters.Add("@userid", SqlDbType.Int).Value = req.userid; command.Parameters.Add("@startdate", SqlDbType.DateTime).Value = req.startdate; command.Parameters.Add("@enddate", SqlDbType.DateTime).Value = req.enddate; command.Parameters.Add("@deliverydate", SqlDbType.DateTime).Value = req.deliverydate; try { SqlDataReader reader = command.ExecuteReader(); } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } foreach (var i in result.Errors) { errors.Add($"ERROR!! {i.PropertyName}: {i.ErrorMessage}"); } return(new JsonResult(errors)); } }