コード例 #1
0
        /// <summary>
        /// Saves a new tenant to the database.
        /// </summary>
        /// <param name="name">Tenants full name, "First Last"</param>
        /// <returns>True if operation was successful and changes were saved.</returns>
        public static bool AddTenant(string id, string name)
        {
            using (var context = new LogsContext())
            {
                context.Database.BeginTransaction();
                try
                {
                    context.Add(
                        new Tenant()
                    {
                        Id   = id,
                        Name = name
                    });

                    context.SaveChanges();
                    context.Database.CommitTransaction();
                }
                catch (Exception)
                {
                    context.Database.RollbackTransaction();
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Saves a log entry to the database.
        /// </summary>
        /// <param name="when">Date and time when this occured, using the format "YY-MM-DD HH:MM:SS"</param>
        /// <param name="doorId">Door id involved.</param>
        /// <param name="eventId">Event id, what happened</param>
        /// <param name="tagId">Tag id used.</param>
        /// <returns>True if operation was successful and changes were saved.</returns>
        public static bool LogEntry(string when, string doorId, string eventId, string tagId)
        {
            using (var context = new LogsContext())
            {
                context.Database.BeginTransaction();
                try
                {
                    context.Add(
                        new LogEntry()
                    {
                        When    = DateTime.ParseExact(when, "yy-MM-dd HH:mm:ss", null),
                        DoorId  = doorId,
                        EventId = eventId,
                        TagId   = tagId
                    }
                        );

                    context.SaveChanges();
                    context.Database.CommitTransaction();
                }
                catch (Exception)
                {
                    context.Database.RollbackTransaction();
                    return(false);
                }
            }

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Moves a tenant from an apartment and updates the tenants tag.
        /// </summary>
        /// <param name="tenantOrTagId">Full name of tenant or tenants tag id. </param>
        /// <param name="apartmentId">Id of apartment to move in to,
        /// or an empty string to simply move out and not into another apartment.</param>
        /// <returns>True if operation was successful and changes were saved.</returns>
        public static bool MoveTenant(string tenantOrTagId, string apartmentId)
        {
            using (var context = new LogsContext())
            {
                try
                {
                    var tenant = context.Tenants
                                 .Where(t => t.Id == tenantOrTagId)
                                 .FirstOrDefault();

                    var tag = context.Tags
                              .Include(t => t.Tenant)
                              .Where(t => t.Id == tenantOrTagId || t.Tenant.Id == tenantOrTagId)
                              .FirstOrDefault();

                    if (tenant == null && tag != null)
                    {
                        tenant = tag.Tenant;
                    }
                    else if (tenant == null && tag == null)
                    {
                        // Tenant not found by tenant id or tag id.
                        return(false);
                    }

                    context.Database.BeginTransaction();
                    try
                    {
                        if (apartmentId == "")
                        {
                            // Tenant moved out. Remove tenant and tag.
                            context.Tenants.Remove(tenant);
                            if (tag != null)
                            {
                                context.Tags.Remove(tag);
                            }
                        }
                        else
                        {
                            // Tenant moved to another apartment.
                            // Set tenants new apartment id.
                            tenant.ApartmentId = apartmentId;

                            if (tag != null)
                            {
                                // Remove old tag if there was one.
                                context.Tags.Remove(tag);
                            }

                            // Set tenants new tag.
                            context.Add(
                                new Tag()
                            {
                                Tenant   = tenant,
                                TenantId = tenant.Id,
                                Id       = TagHelper.GetNextApartmentTag(apartmentId),
                            }
                                );
                        }

                        context.SaveChanges();
                        context.Database.CommitTransaction();
                    }
                    catch (Exception)
                    {
                        context.Database.RollbackTransaction();
                        return(false);
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }