/// <summary> /// Converts database models to a business logic object. /// </summary> public static BusinessLibrary.Models.Dietician Convert(Dietician_db instance) { if (instance == null) { return(null); } return(new BusinessLibrary.Models.Dietician(instance.Id, instance.Dob, instance.Name, instance.Practice, instance.Doctorate, instance.BachelorsDegree, instance.AssociatesDegree, instance.Certification)); }
/// <summary> /// Adds a new instance into the database. /// </summary> public static Dietician_db Add(DateTime dob, string name, string practice, string doctorate, string bachelorsDegree, string associateDegree, string certification, DbContext context, out StatusResponse statusResponse) { try { //validate if (string.IsNullOrEmpty(name?.Trim())) { throw new StatusException(HttpStatusCode.BadRequest, "Please provide a name."); } //Generate a new instance Dietician_db instance = new Dietician_db ( id: Convert.ToInt32(Guid.NewGuid()), dob, name, practice, doctorate, bachelorsDegree, associateDegree, certification ); int rowsAffected = context.ExecuteNonQueryCommand ( commandText: "BEGIN; INSERT INTO dietician (dieticianId, practice, doctorate, bachelors_degree, associate_degree, certification/diploma) values (@id, @practice, @doctorate, @bachelors_degree, @associate_degree, @certification); INSERT INTO user (userId, name, DoB) values (@id, @name, @dob); COMMIT;", parameters: new Dictionary <string, object>() { { "@id", instance.Id }, { "@dob", instance.Dob }, { "@name", instance.Name }, { "@practice", instance.Practice }, { "@doctorate", instance.Doctorate }, { "@bachelors_degree", instance.BachelorsDegree }, { "@associates_degree", instance.AssociatesDegree }, { "@certification", instance.Certification } }, message: out string message ); if (rowsAffected == -1) { throw new Exception(message); } // Return value statusResponse = new StatusResponse("Dietician added successfully"); return(instance); } catch (Exception exception) { statusResponse = new StatusResponse(exception); return(null); } }
/// <summary> /// Retrieves specific instance /// </summary> public static Dietician_db GetDietician(int id, DbContext context, out StatusResponse statusResponse) { try { // Get from database DataTable table = context.ExecuteDataQueryCommand ( commandText: "SELECT * FROM dietician NATURAL JOIN user WHERE dieticianId = @id", parameters: new Dictionary <string, object>() { { "@id", id }, }, message: out string message ); if (table == null) { throw new Exception(message); } // Parse data Dietician_db instances = new Dietician_db(); foreach (DataRow row in table.Rows) { instances = new Dietician_db( id: Convert.ToInt32(row["id"]), dob: Convert.ToDateTime(row["dob"]), name: row["name"].ToString(), practice: row["practice"].ToString(), doctorate: row["doctorate"].ToString(), bachelorsDegree: row["bachelors_degree"].ToString(), associatesDegree: row["associates_degree"].ToString(), certification: row["certification"].ToString() ); } // Return value statusResponse = new StatusResponse("Dietician has been retrieved successfully."); return(instances); } catch (Exception exception) { statusResponse = new StatusResponse(exception); return(null); } }