CodeMate / Programming

CodeMate is back online!

Today I received an email from Azure that my cloud account has been recharged with 25$ for the new month. Which means we’re back! I started to work on making recent progress public. It required a transformation of file serialization to the DB-oriented solution. I encountered only small problems during the transfer, so… it’s ready 🙂

Progress share

I told you lastly that due to unavailable database service I did workaround this with storing data in my local file. But as soon as DB was back alive, I wanted to make it public. And that’s what was needed to perform:

Database access from code

First thing is to create Calendar’s database context. At this point, it holds ‘pointer’ to only one table, but in further development, it would manage all tables around calendar functionality.

namespace CodeMate.WebApp.DAL
{
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using Models;

    public class CalendarDbContext : DbContext
    {
        public CalendarDbContext() : base("defaultConnection")
        {
        }

        public DbSet<CalendarEvent> CalendarEvents { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

The base class constructor is called with the name of the connection string that should be used to access right database – and to be authorized. This setting is present in website service on Azure:

azure connection string

Feeding calendar with sample data

Thereafter, I created context initializer for the time being – to be sure that DB is initializing itself properly. It allows me to see the first event on my calendar before I even add anything:

namespace CodeMate.WebApp.DAL
{
    using System;
    using System.Data.Entity;
    using Models;

    public class CalendarContextInitializer : DropCreateDatabaseIfModelChanges<CalendarDbContext>
    {
        protected override void Seed(CalendarDbContext context)
        {
            var calendarEvent = new CalendarEvent()
            {
                title = "Event initialized from DB",
                Id = "1",
                start = DateTime.UtcNow.ToString(),
                end = DateTime.UtcNow.AddMinutes(45).ToString()
            };

            context.CalendarEvents.Add(calendarEvent);
            context.SaveChanges();
        }
    }
}

Classes above have to be bounded in root Web.config in Entity Framework section:

<entityFramework>
    <contexts>
      <context type="CodeMate.WebApp.DAL.CalendarDbContext, CodeMate.WebApp">
        <databaseInitializer type="CodeMate.WebApp.DAL.CalendarContextInitializer, CodeMate.WebApp" />
      </context>
    </contexts>
    ...
</entityFramework>

Such beauty, wow! 🙂

Aaand finally. Thanks to that, my CalendarEventsRepostory looks far better now 🙂

public class CalendarEventRepository : IRepository<CalendarEvent>
    {
        private readonly CalendarDbContext _calendarDbContext;

        public CalendarEventRepository()
        {
            _calendarDbContext = new CalendarDbContext();
        }

        public IEnumerable<CalendarEvent> List => _calendarDbContext.CalendarEvents.AsEnumerable();

        public void Add(CalendarEvent entity)
        {
            _calendarDbContext.CalendarEvents.Add(entity);
            _calendarDbContext.SaveChanges();
        }

        public void Delete(CalendarEvent entity)
        {
            var eventToDelete = FindById(entity.Id);
            _calendarDbContext.CalendarEvents.Remove(eventToDelete);
            _calendarDbContext.SaveChanges();
        }

        public CalendarEvent FindById(string Id)
        {
            return List.First(x => x.Id == Id);
        }

        public void Update(CalendarEvent entity)
        {
            _calendarDbContext.CalendarEvents.AddOrUpdate(entity);
            _calendarDbContext.SaveChanges();
        }
    }

These actions were enough to bring my app working publicly. You can check the current version out here and add your own event! (Think twice before you name it, deleting is not implemented yet 🙂 )

Please bear in mind that this is not prepared for the input errors (when you put an empty date, bad-formatted date or so). Current application state is rather meant to show that something works there – not to prove that it is unbreakable. So please, hold off your tester’s attitude, breaking this application is pretty simple, don’t try it 🙂

Further plans

Now I believe I need to deliver full CRUD operations for my events, so the user will be able to delete or update existing one. This would probably need another modal dialog, so Martin‘s support may be required. 😀 Meanwhile maybe I’ll manage to secure app’s behavior when a random input is passed. Then I will create another View that will be capable of displaying details of the specific event (another subpage managing that).

Thank you for reading!

Somewhat experienced programmer who tries to find intrinsic meaning in each act he does. Increasingly builds his courage to let his life be driven by passion. Currently giving blogging a whirl.

2 thoughts on “CodeMate is back online!”

  1. Hi Wojciech!

    I’ve gave a bit of time to test your Calendar 🙂

    You are in a good-direction ! 🙂 Hope you will not kill me for those items I”ve created 🙂

    And… I’ve found that you are not reading information about how much time user has selected 🙂 But I’ve hacked it with changing amount of time 🙂

    Great post! PS recently I’ve been on conference about “Azure Serverless” session – you can check samples here : https://github.com/joescars/CostaIoTSensorProject

    Hope to read next article soon! Thanks! 🙂

Leave a Reply