public async Task <BusinessUnitDTO> CreateBusinnesUnitAsync(string brandName, string address, string phoneNumber, string email, string information, int businessUnitCategoryId, int townId, string picture)
        {
            var checkBrandNameIfExists = await this.context.BusinessUnits
                                         .FirstOrDefaultAsync(n => n.Name == brandName);

            if (checkBrandNameIfExists != null)
            {
                throw new AlreadyExistsException(ServicesConstants.BusinessUnitNameAlreadyExists);
            }

            businessValidator.IsNameInRange(brandName);
            businessValidator.IsAddressInRange(address);
            businessValidator.IsEmailValid(email);
            businessValidator.IsPhoneNumberValid(phoneNumber);
            businessValidator.IsDescriptionInRange(information);

            var businessUnit = new BusinessUnit()
            {
                Name = brandName, Address = address, PhoneNumber = phoneNumber, Email = email, Information = information, BusinessUnitCategoryId = businessUnitCategoryId, TownId = townId, Picture = picture
            };

            await this.context.BusinessUnits.AddAsync(businessUnit);

            await this.context.SaveChangesAsync();

            var result = await this.context.BusinessUnits
                         .Include(buc => buc.BusinessUnitCategory)
                         .Include(t => t.Town)
                         .FirstOrDefaultAsync(x => x.Id == businessUnit.Id);

            return(result.ToDTO());
        }
Example #2
0
        public async Task <ReviewDTO> CreateReviewAsync(string originalDescription, int businessUnitId, int rating)
        {
            businessValidator.IsDescriptionInRange(originalDescription);
            businessValidator.IsRatingInRange(rating);

            //automatic edit
            var editedDescription = reviewEditor.AutomaticReviewEditor(originalDescription);

            //check visibility
            var checkVisibility = reviewEditor.CheckReviewVisibility(editedDescription);

            var review = new Review()
            {
                OriginalDescription = originalDescription,
                EditedDescription   = editedDescription,
                Rating         = rating,
                CreatedOn      = DateTime.Now,
                isVisible      = checkVisibility,
                BusinessUnitId = businessUnitId
            };

            this.context.Reviews.Add(review);

            await this.context.SaveChangesAsync();

            var result = await this.context.Reviews
                         .Include(bu => bu.BusinessUnit)
                         .FirstOrDefaultAsync(x => x.Id == review.Id);

            return(result.ToDTO());
        }
        public async Task <NoteDTO> CreateNoteAsync(string userId, int logbookId, string description,
                                                    string image, int?categoryId)
        {
            validator.IsDescriptionIsNullOrEmpty(description);
            validator.IsDescriptionInRange(description);

            var user = await this.context.Users.FindAsync(userId);

            if (user == null)
            {
                throw new NotFoundException(ServicesConstants.UserNotFound);
            }

            var logbook = await this.context.Logbooks.FindAsync(logbookId);

            if (logbook == null)
            {
                throw new NotFoundException(ServicesConstants.LogbookNotFound);
            }

            if (!this.context.UsersLogbooks.Any(x => x.UserId == userId && x.LogbookId == logbookId))
            {
                throw new NotAuthorizedException(string.Format(ServicesConstants.UserNotManagerOfLogbook, user.UserName, logbook.Name));
            }

            var note = new Note()
            {
                Description    = description,
                Image          = image,
                CreatedOn      = DateTime.Now,
                NoteCategoryId = categoryId,
                UserId         = userId,
                LogbookId      = logbookId
            };

            if (categoryId != null)
            {
                var noteCategory = await this.context.NoteCategories.FindAsync(categoryId);

                if (noteCategory == null)
                {
                    throw new NotFoundException(ServicesConstants.NoteCategoryDoesNotExists);
                }

                if (noteCategory.Name == "Task")
                {
                    note.IsActiveTask = true;
                }
            }

            await this.context.Notes.AddAsync(note);

            await this.context.SaveChangesAsync();

            var result = await this.context.Notes
                         .Include(x => x.User)
                         .Include(x => x.NoteCategory)
                         .FirstOrDefaultAsync(x => x.Id == note.Id);

            return(result.ToDTO());
        }