Exemplo n.º 1
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);
        }