public Friend CreateFriend(FriendInputModel body)
        {
            // check if we have connection in db if not set Id to 1 else find the highest Id and add 1 to it
            int nextInt = 0;

            if (_armDbContext.Friends.Count() == 0)
            {
                nextInt = 1;
            }
            else
            {
                nextInt = _armDbContext.Friends.OrderByDescending(a => a.Id).FirstOrDefault().Id + 1;
            }
            var entity = new Friend
            {
                Id        = nextInt,
                FirstName = body.FirstName,
                LastName  = body.LastName,
                Email     = body.Email,
                Phone     = body.Phone,
                Address   = body.Address
            };

            _armDbContext.Friends.Add(entity);
            _armDbContext.SaveChanges();

            return(entity);
        }
Esempio n. 2
0
        public PublicationToFriend CreateFriendBorrowsABookConnection(int userId, int publicationId, PublicationToFriendInputModel body)
        {
            // Check if given user exists
            var friend = _armDbContext.Friends.FirstOrDefault(f => f.Id == userId);

            if (friend == null)
            {
                throw new ResourceNotFoundException($"User with id: {userId} was not found");
            }
            // Check if given publication exists
            var publication = _armDbContext.Publications.FirstOrDefault(p => p.Id == publicationId);

            if (publication == null)
            {
                throw new ResourceNotFoundException($"Publication with id: {publicationId} was not found");
            }
            // check if given user has allready borrowed given publication
            bool alreadyBorrowed = _armDbContext.PublicationsToFriend.Any(p => p.FriendId == friend.Id && p.PublicationId == publication.Id);

            if (alreadyBorrowed)
            {
                throw new ModelFormatException($"User with id: {userId} has already borrowed publication with id: {publicationId}");
            }

            // check if we have connection in db if not set Id to 1 else find the highest Id and add 1 to it
            int nextInt = 0;

            if (_armDbContext.PublicationsToFriend.Count() == 0)
            {
                nextInt = 1;
            }
            else
            {
                nextInt = _armDbContext.PublicationsToFriend.OrderByDescending(a => a.Id).FirstOrDefault().Id + 1;
            }

            var entity = new PublicationToFriend
            {
                Id            = nextInt,
                FriendId      = userId,
                PublicationId = publicationId,
                BorrowDate    = body.BorrowDate.HasValue ? body.BorrowDate : DateTime.Now,
                ReturnDate    = body.ReturnDate
            };

            _armDbContext.PublicationsToFriend.Add(entity);
            _armDbContext.SaveChanges();
            return(entity);
        }
 public void seedDatabase(ArmDbContext dbContext)
 {
     dbContext.Friends.AddRange(friends);
     dbContext.Publications.AddRange(publications);
     dbContext.PublicationsToFriend.AddRange(borrows);
     dbContext.Reviews.AddRange(reviews);
     dbContext.SaveChanges();
 }
        public Review AddUserReviewForPublication(int userId, int publicationId, ReviewInputModel body)
        {
            // Validate that given user exists
            var friend = _armDbContext.Friends.FirstOrDefault(f => f.Id == userId);

            if (friend == null)
            {
                throw new ResourceNotFoundException($"User with id: {userId} was not found");
            }
            // Validate that given publication exists
            var publication = _armDbContext.Publications.FirstOrDefault(p => p.Id == publicationId);

            if (publication == null)
            {
                throw new ResourceNotFoundException($"Publication with id: {publicationId} was not found");
            }
            // Validate that given user has borrowed given publication
            var connection = _armDbContext.PublicationsToFriend.FirstOrDefault(c =>
                                                                               c.FriendId == userId && c.PublicationId == publicationId);

            if (connection == null)
            {
                throw new UserHasNotBorrowedBookException($"User with id: {userId} has not borrowed publication with id: {publicationId} and therefor cannot review it");
            }
            // Validate that given user has not review given publication before
            var review = _armDbContext.Reviews.FirstOrDefault(r => r.FriendId == userId && r.PublicationId == publicationId);

            if (review != null)
            {
                throw new ModelFormatException($"User with id: {userId} has already reviewd publication with id: {publicationId}");
            }

            var newReview = new Review {
                Rating        = body.Rating,
                FriendId      = userId,
                PublicationId = publicationId
            };

            _armDbContext.Reviews.Add(newReview);
            _armDbContext.UpdateRatingForPublication(publicationId);
            _armDbContext.SaveChanges();
            return(newReview);
        }
Esempio n. 5
0
        public Publication CreatePublication(PublicationInputModel body)
        {
            // Validate that given publication does not exists in database
            bool publicationsExists = _armDbContext.Publications.Any(p => p.Isbn == body.Isbn);

            if (publicationsExists)
            {
                throw new ModelFormatException("This publication already Exists");
            }

            // check if we have publications in db if not set Id to 1 else find the highest Id and add 1 to it
            int nextInt = 0;

            if (_armDbContext.Publications.Count() == 0)
            {
                nextInt = 1;
            }
            else
            {
                nextInt = _armDbContext.Publications.OrderByDescending(a => a.Id).FirstOrDefault().Id + 1;
            }
            var entity = new Publication
            {
                Id = nextInt,
                EditorFirstName = body.EditorFirstName,
                EditorLastName  = body.EditorLastName,
                Title           = body.Title,
                Journal         = body.Journal,
                Isbn            = body.Isbn,
                Year            = body.Year,
                Type            = body.Type
            };

            _armDbContext.Publications.Add(entity);
            _armDbContext.SaveChanges();
            return(entity);
        }