public List <PhysicalPersonAttachment> GetPhysicalPersonAttachments(int companyId)
        {
            List <PhysicalPersonAttachment> attachments = new List <PhysicalPersonAttachment>();
            string queryString = selectPart +
                                 "WHERE CompanyId = @CompanyId;";


            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = queryString;
                command.Parameters.Add(new SqlParameter("@CompanyId", companyId));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhysicalPersonAttachment attachment = Read(reader);

                        attachments.Add(attachment);
                    }
                }
            }

            return(attachments);
        }
        public List <PhysicalPersonAttachment> GetPhysicalPersonAttachmentsNewerThen(int companyId, DateTime lastUpdateTime)
        {
            List <PhysicalPersonAttachment> attachments = new List <PhysicalPersonAttachment>();
            string queryString = selectPart +
                                 @"  WHERE CompanyId = @CompanyId 
                    AND CONVERT(DATETIME, CONVERT(VARCHAR(20), UpdatedAt, 120)) > CONVERT(DATETIME, CONVERT(VARCHAR(20), @LastUpdateTime, 120));
                ";


            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = queryString;
                command.Parameters.Add(new SqlParameter("@CompanyId", companyId));
                command.Parameters.Add(new SqlParameter("@LastUpdateTime", lastUpdateTime));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhysicalPersonAttachment attachment = Read(reader);

                        attachments.Add(attachment);
                    }
                }
            }

            return(attachments);
        }
        public static List <PhysicalPersonAttachmentViewModel> ConvertToPhysicalPersonAttachmentViewModelList(this IEnumerable <PhysicalPersonAttachment> PhysicalPersonAttachments)
        {
            List <PhysicalPersonAttachmentViewModel> PhysicalPersonAttachmentViewModels = new List <PhysicalPersonAttachmentViewModel>();

            foreach (PhysicalPersonAttachment PhysicalPersonAttachment in PhysicalPersonAttachments)
            {
                PhysicalPersonAttachmentViewModels.Add(PhysicalPersonAttachment.ConvertToPhysicalPersonAttachmentViewModel());
            }
            return(PhysicalPersonAttachmentViewModels);
        }
        private PhysicalPersonAttachment Read(SqlDataReader reader)
        {
            PhysicalPersonAttachment attachment = new PhysicalPersonAttachment();

            if (reader["AttachmentId"] != DBNull.Value)
            {
                attachment.Id         = Int32.Parse(reader["AttachmentId"].ToString());
                attachment.Identifier = Guid.Parse(reader["AttachmentIdentifier"].ToString());
                attachment.Code       = reader["AttachmentCode"]?.ToString();
                attachment.OK         = bool.Parse(reader["AttachmentOK"].ToString());

                attachment.Active = bool.Parse(reader["Active"].ToString());
            }

            if (reader["PhysicalPersonId"] != DBNull.Value)
            {
                attachment.PhysicalPerson            = new PhysicalPerson();
                attachment.PhysicalPersonId          = Int32.Parse(reader["PhysicalPersonId"].ToString());
                attachment.PhysicalPerson.Id         = Int32.Parse(reader["PhysicalPersonId"].ToString());
                attachment.PhysicalPerson.Identifier = Guid.Parse(reader["PhysicalPersonIdentifier"].ToString());
                attachment.PhysicalPerson.Code       = reader["PhysicalPersonCode"]?.ToString();
                attachment.PhysicalPerson.Name       = reader["PhysicalPersonName"]?.ToString();
            }


            if (reader["CreatedById"] != DBNull.Value)
            {
                attachment.CreatedBy           = new User();
                attachment.CreatedById         = Int32.Parse(reader["CreatedById"].ToString());
                attachment.CreatedBy.Id        = Int32.Parse(reader["CreatedById"].ToString());
                attachment.CreatedBy.FirstName = reader["CreatedByFirstName"]?.ToString();
                attachment.CreatedBy.LastName  = reader["CreatedByLastName"]?.ToString();
            }

            if (reader["CompanyId"] != DBNull.Value)
            {
                attachment.Company      = new Company();
                attachment.CompanyId    = Int32.Parse(reader["CompanyId"].ToString());
                attachment.Company.Id   = Int32.Parse(reader["CompanyId"].ToString());
                attachment.Company.Name = reader["CompanyName"]?.ToString();
            }
            return(attachment);
        }
        public PhysicalPersonAttachment Create(PhysicalPersonAttachment attachment)
        {
            if (context.PhysicalPersonAttachments.Where(x => x.Identifier == attachment.Identifier).Count() == 0)
            {
                attachment.CreatedAt = DateTime.Now;
                attachment.UpdatedAt = DateTime.Now;
                attachment.Active    = true;

                context.PhysicalPersonAttachments.Add(attachment);
                return(attachment);
            }
            else
            {
                var attachmentFromDb = context.PhysicalPersonAttachments.FirstOrDefault(x => x.Identifier == attachment.Identifier);
                if (attachmentFromDb != null)
                {
                    attachmentFromDb.PhysicalPersonId = attachment?.PhysicalPersonId ?? null;
                    attachmentFromDb.OK        = attachment.OK;
                    attachmentFromDb.UpdatedAt = DateTime.Now;
                }
                return(attachmentFromDb);
            }
        }
        public static PhysicalPersonAttachment ConvertToPhysicalPersonAttachment(this PhysicalPersonAttachmentViewModel PhysicalPersonAttachmentViewModel)
        {
            PhysicalPersonAttachment PhysicalPersonAttachment = new PhysicalPersonAttachment()
            {
                Id         = PhysicalPersonAttachmentViewModel.Id,
                Identifier = PhysicalPersonAttachmentViewModel.Identifier,

                Code = PhysicalPersonAttachmentViewModel.Code,

                PhysicalPersonId = PhysicalPersonAttachmentViewModel.PhysicalPerson?.Id ?? null,

                OK     = PhysicalPersonAttachmentViewModel.OK,
                Active = PhysicalPersonAttachmentViewModel.IsActive,

                CreatedById = PhysicalPersonAttachmentViewModel.CreatedBy?.Id ?? null,
                CompanyId   = PhysicalPersonAttachmentViewModel.Company?.Id ?? null,

                CreatedAt = PhysicalPersonAttachmentViewModel.CreatedAt,
                UpdatedAt = PhysicalPersonAttachmentViewModel.UpdatedAt
            };

            return(PhysicalPersonAttachment);
        }
        public static PhysicalPersonAttachmentViewModel ConvertToPhysicalPersonAttachmentViewModelLite(this PhysicalPersonAttachment PhysicalPersonAttachment)
        {
            PhysicalPersonAttachmentViewModel PhysicalPersonAttachmentViewModel = new PhysicalPersonAttachmentViewModel()
            {
                Id         = PhysicalPersonAttachment.Id,
                Identifier = PhysicalPersonAttachment.Identifier,

                Code = PhysicalPersonAttachment.Code,

                OK       = PhysicalPersonAttachment.OK,
                IsActive = PhysicalPersonAttachment.Active,

                UpdatedAt = PhysicalPersonAttachment.UpdatedAt,
                CreatedAt = PhysicalPersonAttachment.CreatedAt
            };

            return(PhysicalPersonAttachmentViewModel);
        }
        public static PhysicalPersonAttachmentViewModel ConvertToPhysicalPersonAttachmentViewModel(this PhysicalPersonAttachment PhysicalPersonAttachment)
        {
            PhysicalPersonAttachmentViewModel PhysicalPersonAttachmentViewModel = new PhysicalPersonAttachmentViewModel()
            {
                Id         = PhysicalPersonAttachment.Id,
                Identifier = PhysicalPersonAttachment.Identifier,

                Code = PhysicalPersonAttachment.Code,

                PhysicalPerson = PhysicalPersonAttachment.PhysicalPerson?.ConvertToPhysicalPersonViewModelLite(),

                OK       = PhysicalPersonAttachment.OK,
                IsActive = PhysicalPersonAttachment.Active,

                CreatedBy = PhysicalPersonAttachment.CreatedBy?.ConvertToUserViewModelLite(),
                Company   = PhysicalPersonAttachment.Company?.ConvertToCompanyViewModelLite(),

                UpdatedAt = PhysicalPersonAttachment.UpdatedAt,
                CreatedAt = PhysicalPersonAttachment.CreatedAt
            };

            return(PhysicalPersonAttachmentViewModel);
        }