/// <summary>
        /// Adds a new instance into the database.
        /// </summary>
        public static CardioLog_db Add(int id, string logName, DateTime logDate, TimeSpan startTime, TimeSpan endTime, int caloriesBurned, string cardioType,
                                       DbContext context, out StatusResponse statusResponse)
        {
            try
            {
                if (id == 0)
                {
                    throw new StatusException(HttpStatusCode.BadRequest, "Please provide a valid id");
                }
                if (logDate == DateTime.MinValue)
                {
                    throw new StatusException(HttpStatusCode.BadRequest, "Please provide a valid date");
                }
                if (startTime == TimeSpan.Zero)
                {
                    throw new StatusException(HttpStatusCode.BadRequest, "Please provide a valid time");
                }
                if (endTime == TimeSpan.Zero)
                {
                    throw new StatusException(HttpStatusCode.BadRequest, "Please provide a valid time");
                }

                //Generate a new instance
                CardioLog_db instance = new CardioLog_db
                                        (
                    id, logName, logDate, startTime, endTime, caloriesBurned, cardioType
                                        );

                //Add to database
                int rowsAffected = context.ExecuteNonQueryCommand
                                   (
                    commandText: "INSERT INTO cardio_log (clientId, log_name, log_date, cardio_type, start_time, end_time, calories_burned) values (@id, @name, @date, @type, @start, @end, @calories)",
                    parameters: new Dictionary <string, object>()
                {
                    { "@id", instance.Id },
                    { "@name", instance.LogName },
                    { "@date", instance.LogDate },
                    { "@start", instance.StartTime },
                    { "@end", instance.EndTime },
                    { "@calories", instance.CaloriesBurned },
                    { "@type", instance.CardioType }
                },
                    message: out string message
                                   );
                if (rowsAffected == -1)
                {
                    throw new Exception(message);
                }

                //Return value
                statusResponse = new StatusResponse("Cardio Log added successfully");
                return(instance);
            }
            catch (Exception exception)
            {
                statusResponse = new StatusResponse(exception);
                return(null);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Converts database models to a business logic object.
 /// </summary>
 public static BusinessLibrary.Models.CardioLog Convert(CardioLog_db instance)
 {
     if (instance == null)
     {
         return(null);
     }
     return(new BusinessLibrary.Models.CardioLog(instance.Id, instance.LogDate, instance.Time, instance.CaloriesBurned, instance.CardioType));
 }
        /// <summary>
        /// Retrieves specific instance
        /// </summary>
        public static CardioLog_db GetCardioLog(int id, DateTime date, TimeSpan time,
                                                DbContext context, out StatusResponse statusResponse)
        {
            try
            {
                //Get from database
                DataTable table = context.ExecuteDataQueryCommand
                                  (
                    commandText: "SELECT * FROM cardioLogs WHERE id = @id, date = @date, time = @time",
                    parameters: new Dictionary <string, object>()
                {
                    { "@id", id },
                    { "@date", date },
                    { "@time", time }
                },
                    message: out string message
                                  );

                if (table == null)
                {
                    throw new Exception(message);
                }

                //Parse data
                CardioLog_db instance = new CardioLog_db();
                foreach (DataRow row in table.Rows)
                {
                    instance = new CardioLog_db
                               (
                        id: Convert.ToInt32(row["id"]),
                        logName: row["name"].ToString(),
                        logDate: Convert.ToDateTime(row["date"]),
                        startTime: TimeSpan.Parse(row["start_time"].ToString()),
                        endTime: TimeSpan.Parse(row["end_time"].ToString()),
                        caloriesBurned: Convert.ToInt32(row["calories_burned"]),
                        cardioType: row["cardio_type"].ToString()
                               );
                }

                //Return value
                statusResponse = new StatusResponse("Cardio Log successfully retrieved");
                return(instance);
            }
            catch (Exception exception)
            {
                statusResponse = new StatusResponse(exception);
                return(null);
            }
        }