public async Task DeleteAsync(long id)
 {
     _context.Recipes.Remove(new Recipe {
         Id = id
     });
     await _context.SaveChangesAsync();
 }
        public async Task <Int32> AddRecipe(Recipe recipe)
        {
            await _context.Recipes.AddAsync(recipe);

            await _context.SaveChangesAsync();

            return(recipe.Id);
        }
Ejemplo n.º 3
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var recipe = await context.Recipes.FindAsync(request.Id, cancellationToken);

                if (recipe is null)
                {
                    throw new RestException(System.Net.HttpStatusCode.NotFound, new { message = "Could not find recipe" });
                }

                var user = await context.Users.SingleOrDefaultAsync(x => x.UserName == currentUserAccessor.GetCurrentUsername(), cancellationToken : cancellationToken);

                var favorite = await context.RecipeFavorites.SingleOrDefaultAsync(x => x.RecipeId == recipe.Id && x.UserId == user.Id, cancellationToken : cancellationToken);

                if (favorite == null)
                {
                    return(Unit.Value);
                }

                context.RecipeFavorites.Remove(favorite);

                if (await context.SaveChangesAsync(cancellationToken) > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var recipe = await context.Recipes.FindAsync(new object[] { request.Id }, cancellationToken : cancellationToken);

                if (recipe == null)
                {
                    throw new RestException(System.Net.HttpStatusCode.NotFound, new { message = "Could not find recipe" });
                }

                var user = await context.Users.SingleOrDefaultAsync(x => x.UserName == currentUserAccessor.GetCurrentUsername(), cancellationToken : cancellationToken);

                var favorited = await context.RecipeFavorites.SingleOrDefaultAsync(x => x.RecipeId == recipe.Id && x.UserId == user.Id, cancellationToken : cancellationToken);

                if (favorited != null)
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { message = "Already added to favorite" });
                }

                favorited = new Domain.RecipeFavorite
                {
                    Recipe = recipe,
                    User   = user
                };

                context.RecipeFavorites.Add(favorited);

                if ((await context.SaveChangesAsync(cancellationToken) > 0))
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 5
0
        public async Task <User> AddUser(User user)
        {
            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(user);
        }
Ejemplo n.º 6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                if (string.IsNullOrEmpty(request.Token))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Message = "Token must not be empty" });
                }

                var user = await context.AppUsers
                           .Include(x => x.RefreshTokens)
                           .SingleOrDefaultAsync(u => u.RefreshTokens.Any(t => t.Token == request.Token),
                                                 cancellationToken: cancellationToken);

                if (user == null)
                {
                    throw new RestException(System.Net.HttpStatusCode.NotFound);
                }

                var refreshToken = user.RefreshTokens.Single(x => x.Token == request.Token);

                if (!refreshToken.IsActive)
                {
                    throw new RestException(System.Net.HttpStatusCode.Unauthorized);
                }

                refreshToken.Revoked     = DateTime.UtcNow;
                refreshToken.RevokedByIp = request.Ip;

                context.Update(user);
                await context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Ejemplo n.º 7
0
            public async Task <CommentDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByNameAsync(currentUserAccessor.GetCurrentUsername());

                var recipe = await context.Recipes.FirstOrDefaultAsync(x => x.Id == request.RecipeId, cancellationToken : cancellationToken);

                if (recipe == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { message = "Recipe not found" });
                }

                var comment = new Comment()
                {
                    Body      = request.Body,
                    Recipe    = recipe,
                    Author    = user,
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now,
                    Id        = Guid.NewGuid().ToString()
                };

                await context.Comments.AddAsync(comment, cancellationToken);

                if (await context.SaveChangesAsync(cancellationToken) > 0)
                {
                    return(mapper.Map <Comment, CommentDto>(comment));
                }

                throw new Exception("Problem when creating comment");
            }
Ejemplo n.º 8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var comment = await context.Comments.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { message = "Comment not found" });
                }

                if (comment.Body.Equals(request.Body))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { message = "Comment body didn't change" });
                }

                comment.Body = request.Body;

                if (context.ChangeTracker.Entries().First(x => x.Entity == comment).State == EntityState.Modified)
                {
                    comment.UpdatedAt = DateTime.Now;
                }

                if (await context.SaveChangesAsync(cancellationToken) <= 0)
                {
                    throw new Exception("Problem saving changes");
                }

                return(Unit.Value);
            }
Ejemplo n.º 9
0
            public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var recipes = await context.Recipes
                    .Include(x => x.Categories)
                    .Include(x => x.Cuisine)
                    .Include(x => x.Ingredients)
                    .Include(x => x.NutritionValue)
                    .Include(x => x.StepsOfCooking)
                    .ToListAsync(cancellationToken: cancellationToken);

                var recipe = recipes.Find(x => x.Id == request.Id);

                if (recipe == null)
                    throw new RestException(System.Net.HttpStatusCode.NotFound, new { Recipe = "Recipe not found" });

                Domain.Difficulty diff = recipe.Difficulty;
                object obj;
                if (Enum.TryParse(typeof(Domain.Difficulty), request.Difficulty, true, out obj) && obj is Domain.Difficulty difficulty)
                {
                    diff = difficulty;
                }

                recipe.Servings = request.Servings ?? recipe.Servings;
                recipe.Title = request.Title ?? recipe.Title;
                recipe.TimeOfCooking = request.TimeOfCooking ?? recipe.TimeOfCooking;
                recipe.Ingredients = request.Ingredients ?? recipe.Ingredients;
                recipe.Description = request.Description;
                recipe.Cuisine = context.Cuisines.FirstOrDefault(x => x.Name == request.Cuisine) ?? recipe.Cuisine;
                recipe.Difficulty = diff;
                recipe.NutritionValue = request.NutritionValue ?? recipe.NutritionValue;
                recipe.StepsOfCooking = request.StepsOfCooking ?? recipe.StepsOfCooking;

                var categories = new List<Category>();
                foreach (var cat in request.Categories)
                {
                    categories.Add(context.Categories.FirstOrDefault(x => x.Name == cat.Name));
                }

                recipe.Categories = categories ?? recipe.Categories;


                if (context.ChangeTracker.Entries().First(x => x.Entity == recipe).State == EntityState.Modified)
                {
                    recipe.UpdatedAt = DateTime.Now;
                }

                if(await context.SaveChangesAsync(cancellationToken) > 0)
                    return Unit.Value;

                throw new Exception("Problem saving recipe");

            }
Ejemplo n.º 10
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var result = photoAccessor.CreatePhoto(request.File);

                var user = await userManager.FindByNameAsync(currentUserAccessor.GetCurrentUsername());

                user.Photo = result;

                context.Images.Add(user.Photo);

                await context.SaveChangesAsync(cancellationToken);

                return(user.Photo);
            }
Ejemplo n.º 11
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                if (string.IsNullOrEmpty(request.Token))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Message = "Token must not be empty" });
                }

                var user = await context.AppUsers
                           .Include(x => x.RefreshTokens)
                           .Include(x => x.Photo)
                           .SingleOrDefaultAsync(u => u.RefreshTokens.Any(t => t.Token == request.Token),
                                                 cancellationToken: cancellationToken);

                if (user == null)
                {
                    throw new RestException(System.Net.HttpStatusCode.NotFound);
                }

                var refreshToken = user.RefreshTokens.Single(x => x.Token == request.Token);

                if (!refreshToken.IsActive)
                {
                    throw new RestException(System.Net.HttpStatusCode.Forbidden);
                }

                var newRefreshToken = refreshTokenGenerator.GenerateRefreshToken(request.Ip);

                refreshToken.Revoked         = DateTime.Now;
                refreshToken.RevokedByIp     = request.Ip;
                refreshToken.ReplacedByToken = newRefreshToken.Token;
                user.RefreshTokens.Add(newRefreshToken);

                context.Update(user);
                await context.SaveChangesAsync(cancellationToken);

                var jwtToken = await jwtGenerator.CreateToken(user);

                return(new User()
                {
                    Username = user.UserName,
                    ImageUrl = user.Photo.Url,
                    JwtToken = jwtToken,
                    RefreshToken = newRefreshToken.Token
                });
            }
Ejemplo n.º 12
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await context.Categories.Where(x => x.Name == request.Name).AnyAsync(cancellationToken: cancellationToken)))
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { Category = "Category doesn`t exist" });
                }


                var cat = context.Categories.FirstOrDefault(x => x.Name == request.Name);

                context.Categories.Remove(cat);

                if (await context.SaveChangesAsync(cancellationToken) > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem deleting category");
            }
Ejemplo n.º 13
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var comment = await context.Comments.FirstOrDefaultAsync(x => x.Id == request.Id,
                                                                         cancellationToken : cancellationToken);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { message = "Comment not found" });
                }

                context.Comments.Remove(comment);

                if (await context.SaveChangesAsync(cancellationToken) <= 0)
                {
                    throw new Exception("Problem saving changes");
                }

                return(Unit.Value);
            }
Ejemplo n.º 14
0
        public async System.Threading.Tasks.Task <bool> DeletePhotoAsync(Photo photo)
        {
            File.Delete(photo.Path);
            await context.Users.LoadAsync();

            context.Images.Remove(photo);

            try
            {
                if (await context.SaveChangesAsync() > 0)
                {
                    return(true);
                }
            }
            catch (Exception)
            {
                throw;
            }
            throw new Exception("Problem deleting image");
        }
Ejemplo n.º 15
0
            public async Task <Cuisine> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await context.Cuisines.Where(x => x.Name == request.Name).AnyAsync(cancellationToken: cancellationToken))
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { Cuisine = "Cuisine already exists" });
                }

                var cuisine = new Cuisine()
                {
                    Name = request.Name,
                    Id   = Guid.NewGuid().ToString()
                };

                context.Cuisines.Add(cuisine);

                if (await context.SaveChangesAsync(cancellationToken) > 0)
                {
                    return(cuisine);
                }

                throw new Exception("Problem creating cuisine");
            }