Example #1
0
        public bool Add(AuthorBLL item, out string serverSideError)
        {
            if (item.IsValid)
            {
                using (ProtoLibEntities context = new ProtoLibEntities())
                {
                    if (DatabaseDependantValidation(item, context, out serverSideError))
                    {
                        Author newAuth = new Author();

                        //newAuth.BookDetailsSet = whoa... Okay, remember.. first authors are added, then new books and author specified there

                        CrossLayerEntityConverter.AuthorBllToDal(context, item, newAuth);
                        context.Authors.AddObject(newAuth);

                        context.SaveChanges();

                        return true;
                    }
                }

                return false;
            }
            else
            {
                serverSideError = "The Author object is invalid";
                return false;
            }
        }
        public static void AuthorBllToDal(ProtoLibEntities context, AuthorBLL bllAuthor, Author dalAuthor)
        {
            dalAuthor.FirstName = bllAuthor.FirstName;
            dalAuthor.MiddleName = bllAuthor.MiddleName;
            dalAuthor.LastName = bllAuthor.LastName;
            dalAuthor.DateOfBirth = bllAuthor.DateOfBirth;
            dalAuthor.DateOfDeath = bllAuthor.DateOfDeath;
            dalAuthor.Nationality = bllAuthor.Nationality;

            //FIXME: copy image binary data to BLL object
            dalAuthor.AuthorImage = bllAuthor.AuthorImage;

            foreach (int bookID in bllAuthor.BookDetailsIDs)
                dalAuthor.BookDetailsSet.Add(context.BookDetailsSet.Single(b => b.BookDetailsID == bookID));
        }
        public static void AuthorDalToBll(ProtoLibEntities context, AuthorBLL bllAuthor, Author dalAuthor)
        {
            bllAuthor.ItemID = dalAuthor.AuthorID;

            bllAuthor.FirstName = dalAuthor.FirstName;
            bllAuthor.MiddleName = dalAuthor.MiddleName;
            bllAuthor.LastName = dalAuthor.LastName;
            bllAuthor.DateOfBirth = dalAuthor.DateOfBirth;
            bllAuthor.DateOfDeath = dalAuthor.DateOfDeath;
            bllAuthor.Nationality = dalAuthor.Nationality;

            bllAuthor.AuthorImage = dalAuthor.AuthorImage;

            foreach (BookDetails b in dalAuthor.BookDetailsSet)
                bllAuthor.BookDetailsIDs.Add(b.BookDetailsID);
        }
            public AuthorSummary(AuthorBLL author)
            {
                ID = author.ItemID;

                Name = author.FirstName + " " + author.MiddleName +
                    " " + author.LastName;

                if (author.DateOfBirth.HasValue)
                {
                    TimeSpan ts = DateTime.Now - (DateTime)author.DateOfBirth;
                    Age = ts.Days / 365;
                }

                Nationality = author.Nationality;
            }
Example #5
0
        public AuthorBLL GetByID(int id)
        {
            using (ProtoLibEntities context = new ProtoLibEntities())
            {
                Author dalAuthor = (from a in context.Authors
                                    where a.AuthorID == id
                                    select a).SingleOrDefault();

                if (dalAuthor != null)
                {
                    AuthorBLL bllAuthor = new AuthorBLL();
                    CrossLayerEntityConverter.AuthorDalToBll(context, bllAuthor, dalAuthor);
                    return bllAuthor;
                }

            }

            return null;
        }
Example #6
0
        private bool DatabaseDependantValidation(AuthorBLL bllAuthor, ProtoLibEntities context, 
            out string error,
            int idToExclude = 0)
        {
            error = null;

            return true;
        }
Example #7
0
        public bool Update(AuthorBLL newItem, out string serverSideError)
        {
            if (newItem.IsValid)
            {
                using (ProtoLibEntities context = new ProtoLibEntities())
                {
                    if (DatabaseDependantValidation(newItem, context, out serverSideError, newItem.ItemID))
                    {
                        Author dalAuth = (from a in context.Authors
                                          where a.AuthorID == newItem.ItemID
                                          select a).Single();

                        CrossLayerEntityConverter.AuthorBllToDal(context, newItem, dalAuth);
                        context.SaveChanges();

                        Debug.Assert(serverSideError == null);
                        return true;
                    }
                    else
                        return false;
                }
            }

            serverSideError = "Item is in an invalid state!";
            return false;
        }
Example #8
0
        public List<AuthorBLL> GetPreviousItems(int beforeItemID, int numItems)
        {
            List<AuthorBLL> retList = new List<AuthorBLL>();

            using (ProtoLibEntities context = new ProtoLibEntities())
            {
                var dalAuthors = (from a in context.Authors
                                where a.AuthorID < beforeItemID
                                orderby a.AuthorID descending
                                select a).Take(numItems);

                AuthorBLL bllAuthor = null;
                foreach (Author dalAuthor in dalAuthors)
                {
                    bllAuthor = new AuthorBLL();
                    CrossLayerEntityConverter.AuthorDalToBll(context, bllAuthor, dalAuthor);
                    retList.Add(bllAuthor);
                }
            }

            retList.Reverse();
            return retList;
        }
Example #9
0
        public List<AuthorBLL> GetFirstItems(int numItems)
        {
            List<AuthorBLL> retList = new List<AuthorBLL>();

            using (ProtoLibEntities context = new ProtoLibEntities())
            {
                var dalAuthors = (from a in context.Authors
                                orderby a.AuthorID
                                select a).Take(numItems);

                AuthorBLL bllAuthor = null;
                foreach (Author dalAuthor in dalAuthors)
                {
                    bllAuthor = new AuthorBLL();
                    CrossLayerEntityConverter.AuthorDalToBll(context, bllAuthor, dalAuthor);
                    retList.Add(bllAuthor);
                }
            }

            return retList;
        }
Example #10
0
        public List<AuthorBLL> GetByName(string name, int numResults)
        {
            List<AuthorBLL> resultList = new List<AuthorBLL>();

            if (name != null)
            {
                string[] nameParts = name.Split(' ');

                using (ProtoLibEntities context = new ProtoLibEntities())
                {

                    //ALT: Get a better authors result using strength of result etc
                    var dalAuthors = (from a in context.Authors
                                      where nameParts.Any(val => ((a.FirstName + " " + ((a.MiddleName == null)? "":a.MiddleName) +
                                                                   " " + a.LastName).Contains(val)))
                                      select a).Take(numResults);

                    foreach (Author dalAuthor in dalAuthors)
                    {
                        AuthorBLL bllAuthor = new AuthorBLL();
                        CrossLayerEntityConverter.AuthorDalToBll(context, bllAuthor, dalAuthor);
                        resultList.Add(bllAuthor);
                    }
                }
            }

            return resultList;
        }