示例#1
0
        public void RemoveLink(long userId, long linkId)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.UserProfile.userId != userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, "linkId", linkId);
            }

            try
            {
                LinkDao.Remove(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
示例#2
0
        public LinkDetails GetLink(long linkId)
        {
            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex);
            }

            UserProfile user;

            try
            {
                user = UserDao.Find(link.userId);
            }
            catch (InstanceNotFoundException <UserProfile> ex)
            {
                throw new InternalErrorException(ex);
            }

            int rating = RatingDao.CalculateValueForLink(link.linkId);

            return(new LinkDetails(link.linkId, link.userId, user.userLogin, link.movieId, link.name, link.description, link.url, rating, link.reportRead.GetValueOrDefault(), link.date));
        }
示例#3
0
        public void SetReportedLinkAsRead(long userId, long linkId)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (userId != link.UserProfile.userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, "linkId", linkId);;
            }

            link.reportRead = true;

            try
            {
                LinkDao.Update(link);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
示例#4
0
        public void UpdateLink(long userId, long linkId, string name, string description)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if ((link.name != name) && (LinkDao.ExistsForMovieAndName(link.movieId, name)))
            {
                throw new DuplicateInstanceException <LinkDetails>("movieId", link.movieId, "name", name);
            }

            if (userId != link.UserProfile.userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, "linkId", linkId);;
            }

            link.name        = name;
            link.description = description;

            try
            {
                LinkDao.Update(link);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
示例#5
0
        public long Rate(long userId, long linkId, int value)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.UserProfile.userId == userId)
            {
                throw new UserNotAuthorizedException <RatingDetails>(userId, "linkId", linkId);
            }

            if (!RatingDao.ExistsForUserAndLink(userId, linkId))
            {
                if (value == 0)
                {
                    return(-1);
                }

                Rating rating = Rating.CreateRating(-1, userId, linkId, value, DateTime.Now);
                try
                {
                    RatingDao.Create(rating);
                }
                catch (DuplicateInstanceException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                return(rating.ratingId);
            }
            else
            {
                Rating rating;
                try
                {
                    rating = RatingDao.FindForUserAndLink(userId, linkId);
                }
                catch (InstanceNotFoundException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }
                catch (DuplicateInstanceException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                if (value == 0)
                {
                    try
                    {
                        RatingDao.Remove(rating.ratingId);
                    }
                    catch (InstanceNotFoundException <Rating> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                    return(-1);
                }

                rating.value = value;
                try
                {
                    RatingDao.Update(rating);
                }
                catch (InstanceNotFoundException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                return(rating.ratingId);
            }
        }
示例#6
0
        public void RemoveLabelsForLink(long userId, long linkId)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.UserProfile.userId != userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, linkId);
            }

            List <Label> removableLabels = new List <Label>();

            foreach (Label label in link.Labels)
            {
                if (label.Links.Contains(link))
                {
                    removableLabels.Add(label);
                }
            }

            foreach (Label label in removableLabels)
            {
                label.Links.Remove(link);
                link.Labels.Remove(label);

                LinkDao.Update(link);

                if (label.Links.Count > 0)
                {
                    try
                    {
                        LabelDao.Update(label);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
                else
                {
                    try
                    {
                        LabelDao.Remove(label.labelId);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
            }
        }
示例#7
0
        public void SetLabelsForLink(long userId, long linkId, List <string> labelTexts)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.userId != userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, "linkId", linkId);
            }

            List <Label> labelsToSet = new List <Label>();

            foreach (string labelText in labelTexts.Distinct())
            {
                if (labelText != null)
                {
                    string trimmedLabelText = labelText.Trim();

                    if (trimmedLabelText != "")
                    {
                        Label label;
                        try
                        {
                            label = LabelDao.FindByText(trimmedLabelText);
                        }
                        catch (InstanceNotFoundException <Label> )
                        {
                            label = Label.CreateLabel(-1, trimmedLabelText);
                            LabelDao.Create(label);
                        }
                        catch (DuplicateInstanceException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                        if (!labelsToSet.Contains(label))
                        {
                            labelsToSet.Add(label);
                        }
                    }
                }
            }

            List <Label> labelsAlreadyForLink;

            try
            {
                labelsAlreadyForLink = LabelDao.FindForLink(linkId);
            }
            catch (InstanceNotFoundException <Label> )
            {
                labelsAlreadyForLink = new List <Label>();
            }
            foreach (Label labelAlreadyForLink in labelsAlreadyForLink)
            {
                if (!labelsToSet.Contains(labelAlreadyForLink))
                {
                    link.Labels.Remove(labelAlreadyForLink);

                    if (labelAlreadyForLink.Links.Count == 1)
                    {
                        labelAlreadyForLink.Links.Remove(link);
                        try
                        {
                            LabelDao.Update(labelAlreadyForLink);
                        }
                        catch (InstanceNotFoundException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            LabelDao.Remove(labelAlreadyForLink.labelId);
                        }
                        catch (InstanceNotFoundException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                    }
                }
                else
                {
                    labelsToSet.Remove(labelAlreadyForLink);
                }
            }

            foreach (Label label in labelsToSet)
            {
                if (!link.Labels.Contains(label))
                {
                    link.Labels.Add(label);

                    label.Links.Add(link);
                    try
                    {
                        LabelDao.Update(label);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
            }

            try
            {
                LinkDao.Update(link);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
示例#8
0
 public Link FindLink(long linkId)
 {
     return(LinkDao.Find(linkId));
 }