Categories
Software Development Uncategorized

Setup Cron Job

crontab -l (list)crontab: no crontab for james
export EDITOR=nano   //default vi
crontab -e (edit)
* * * * * command to be executed

– – – – –

| | | | |

| | | |+—– day of week (0 – 6) (where 0 represents Sunday and 6 represents Saturday)

| | | +——- month (1 – 12)

| | +——— day of month (1 – 31)

| +———– hour (0 – 23)+————- min (0 – 59)

Hourly at ‘0’


// every 15 minutes

0,15,30,45 * * * * cd /Users/james/Sites/local/api/sensor/; php -q GenerateWaterEventPush.php


// every 5 minutes

0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /Users/james/Sites/local/api/sensor/; php -q GenerateWaterEventPush.php


// Stop cron job  -> crontab -e -> remove above -> save -> do crontab -l to verify

Categories
Software Development Uncategorized

Setup and pass data through a simple ASP.NET MVC app

Creating a new data driven MVC Application (MyAppMVC) using Entity Framework

Add projects

MyAppMVC.Data.Model for the data model and MyAppMVC.Data for the DataContext (with dbset<> ) and migrations folder (include configuration.cs and then add a Seeder class)

Add an Adapters Folder with subfolders Data, Interface and Mock to the main (startup) project.

Add References between the projects:

MyAppMVC references MyAppMVC.Data and MyAppMVC.Data.Model

MyAppMVC.Data references MyAppMVC.Data.Model

Unit test References if needed

Add the Entity Framework Package (NuGet)

Add Data Model classes to .Data.Model

Add ——Context.cs class to .Data (it is a :DbContext)

in this class add DbSets (public DbSet<—-> ——-s { get; set; }

In Package Manager enable-migrations under .Data — a Migrations folder with

Configuration.cs file is added

Seed data via Configuration.cs

Add a Seeder.Seed(context) to Configuration and create a Seeder file with a Seed method that takes the dbcontext as a parameter. using System.Data.Entity.Migrations (for AddOrUpdate extension method)

public static void Seed(—–Context context)

        {

            context.——.AddOrUpdate(c => c.—-, new —- { Name=”James” });

        }

add-migration initial and then update-database. Open SQL Server Management Studio or VS Database Explorer and celebrate.  VS use (LocalDb)\v11.0

To move data up the stack we use an adapter class to fetch data from the database and return a view model. 


Create a new cotroller (Home) and right click then add a view(Index).

Next add a view model that contains the data the view needs. (Right click Model (top level) and add a .cs class called ——-ViewModel ).  Add the data needed from using .Data.Model project .

Right click Interface in Adapter and add an I—-Adapter interface that has a method Get——ViewModel that returns the desired view model.  Implement the interface in the Data (and Mock) folders.

 public class —-Adapter : —–.Adapter.Interface.I—-Adapter

    {

        public Models.——ViewModel Get——ViewModel()

        {

// create the viewmodel and dbcontext to work with

            —-Context db = new —-Context();

            —–ViewModel model = new —–ViewModel();

            model.—– = db.—–.ToList();    //.FirstOrDefault() for 1 item

// filter with .Where and use .take(#) .orderby() ……

            return model;      

        }

    }

// the adapter queries the database and returns our view model

Now we utilize the adapter in the Controller Action and return the view model to the view.

 —–Adapter _adapter;

        public HomeController()

        {

            _adapter = new —–Adapter();

        }

        public ActionResult Index()

        {

            return View(_adapter.Get——ViewModel());

        }

In the view we specify a view model

@model —–MVC.Models.—–ViewModel        

to loop through a List item with razor

@foreach(var element in Model.—–)

{

@element.—–

}

We have now built a database using code first and the Entity Framework. We then queried that database and passed the results up to a view model.