Exemple #1
0
        public async Task <ResultDto> UpdateSharedMeal(SharedMealDto sharedMealDto)
        {
            var sharedMeal = await _context.SharedMeals.Where(sm => sm.UUID == sharedMealDto.UUID).FirstOrDefaultAsync();

            if (sharedMeal == null)
            {
                _logger.LogDebug("SharedMeal not found with UUID: ", sharedMealDto.UUID);
                return(new ResultDto(false, "Meal not found"));
            }

            sharedMeal.Info      = sharedMealDto.Info;
            sharedMeal.Name      = sharedMealDto.Name;
            sharedMeal.Recipe    = sharedMealDto.Recipe;
            sharedMeal.Tags      = sharedMealDto.Tags;
            sharedMeal.UpdatedAt = DateTime.Now;
            sharedMeal.Shared    = sharedMeal.Shared;

            List <Food> foods = _foodService.MapFoodDtoListToFoodList(sharedMealDto.Foods);

            _context.SharedMealFoods.RemoveRange(_context.SharedMealFoods.Where(smf => smf.SharedMealId == sharedMeal.Id));
            foods.ForEach(f => {
                var smf = new SharedMealFood
                {
                    FoodId     = f.Id,
                    SharedMeal = sharedMeal,
                    FoodAmount = sharedMealDto.Foods.FindLast(food => food.UUID == f.UUID).Amount
                };
                _context.Add(smf);
            });

            _context.Entry(sharedMeal).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Meal updated succesfully"));
        }
Exemple #2
0
        public async Task <ResultDto> ForgotPassword(string usernameOrEmail)
        {
            var foundUser = await _context.Users
                            .Where(u => u.Email == usernameOrEmail || u.Username == usernameOrEmail)
                            .FirstOrDefaultAsync();

            if (foundUser != null && foundUser.Email != null)
            {
                foundUser.PasswordResetToken    = Guid.NewGuid().ToString();
                _context.Entry(foundUser).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                var result = await SendTokenViaEmail(foundUser.Email, foundUser.PasswordResetToken);

                if (!result)
                {
                    foundUser.PasswordResetToken    = null;
                    _context.Entry(foundUser).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(new ResultDto(false, "Unable to sent email"));
                }

                return(new ResultDto(true, "Password reset token sent to " + foundUser.Email));
            }

            return(new ResultDto(false, "Something went wrong"));
        }
Exemple #3
0
        public async Task <ResultDto> UpdateFoodInformation(FoodDto foodDto)
        {
            var food = await _context.Foods.Where(f => f.UUID == foodDto.UUID).FirstOrDefaultAsync();

            if (food == null)
            {
                _logger.LogDebug("Food not found with id: ", foodDto.UUID);
                return(new ResultDto(false, "Food not found"));
            }

            food.UpdatedAt   = DateTime.Now;
            food.Name        = foodDto.Name ?? food.Name;
            food.Energy      = foodDto.Energy;
            food.Carbs       = foodDto.Carbs;
            food.Fat         = foodDto.Fat;
            food.Protein     = foodDto.Protein;
            food.Fiber       = foodDto.Fiber;
            food.Sugar       = foodDto.Sugar;
            food.PackageSize = foodDto.PackageSize;
            food.ServingSize = foodDto.ServingSize;

            _context.Entry(food).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Food updated succesfully"));
        }
Exemple #4
0
        public async Task <ResultDto> AnswerToFeedback(FeedbackDto feedbackDto, string userId)
        {
            var feedback = await _context.Feedbacks.Where(f => f.UUID == feedbackDto.UUID).FirstOrDefaultAsync();

            if (feedback != null)
            {
                if (feedback.Answer != null)
                {
                    feedback.AnswerUpdatedAt = DateTime.Now;
                }
                else
                {
                    feedback.AnsweredAt = DateTime.Now;
                }

                feedback.Answer     = feedbackDto.Answer;
                feedback.AnsweredBy = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync();

                _context.Entry(feedback).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(new ResultDto(true, "Answer added succesfully"));
            }

            return(new ResultDto(false, "Feedback not found"));
        }
Exemple #5
0
        public async Task <ResultDto> UpdateDay(DayDto dayDto, string userId)
        {
            Day currentDay = await _context.Days.Where(d => d.UUID == dayDto.UUID && d.User.UUID == userId)
                             .Include(d => d.User)
                             .Include(d => d.Meals)
                             .FirstOrDefaultAsync();

            if (currentDay == null)
            {
                _logger.LogDebug("Day not found with UUID: ", dayDto.UUID);
                return(new ResultDto(false, "Day not found"));
            }

            Day newDay = _mapper.Map <Day>(dayDto);

            newDay.Name        = currentDay.Name;
            newDay.User        = currentDay.User;
            newDay.CreatedAt   = currentDay.CreatedAt;
            newDay.Meals       = _mealService.AddMeals(dayDto.AllMeals, userId);
            newDay.UUID        = Guid.NewGuid().ToString();
            newDay.Date        = currentDay.Date != null ? currentDay.Date : currentDay.CreatedAt;
            newDay.UpdatedAt   = DateTime.Now;
            newDay.IsLatest    = true;
            newDay.HasVersions = true;

            currentDay.Name            = currentDay.Name;
            currentDay.IsLatest        = false;
            currentDay.VersionCreated  = DateTime.Now;
            currentDay.UpdatedAt       = DateTime.Now;
            currentDay.Date            = dayDto.Date == null && currentDay.Date == null ? currentDay.CreatedAt : dayDto.Date;
            currentDay.LatestVersionId = newDay.UUID;
            currentDay.HasVersions     = true;

            _context.Add(newDay);
            _context.Entry(currentDay).State = EntityState.Modified;

            var days = await _context.Days.Where(d => d.LatestVersionId == currentDay.UUID).ToListAsync();

            days.ForEach(d => d.LatestVersionId = newDay.UUID);

            await _context.SaveChangesAsync();

            return(new ResultDto(true, newDay.UUID));
        }
Exemple #6
0
        public async Task <ResultDto> UpdateTrackedPeriod(NewTrackedPeriodDto trackedPeriodDto, string userId)
        {
            var tp = await _context.TrackedPeriods.Where(tr => tr.UUID == trackedPeriodDto.UUID).FirstOrDefaultAsync();

            if (tp == null)
            {
                _logger.LogDebug("SharedMeal not found with UUID: ", trackedPeriodDto.UUID);
                return(new ResultDto(false, "Tracked Period not found"));
            }

            var days = new List <Day>();

            trackedPeriodDto.DayIds.ForEach(d => days.Add(
                                                _context.Days.Where(day => day.UUID == d && day.User.UUID == userId)
                                                .Include(day => day.Meals)
                                                .ThenInclude(m => m.MealFoods)
                                                .ThenInclude(mf => mf.Food)
                                                .FirstOrDefault()
                                                )
                                            );

            if (days == null || days.Count == 0)
            {
                return(new ResultDto(false, "No days found with given information"));
            }

            _context.TrackedPeriodDays.RemoveRange(_context.TrackedPeriodDays.Where(tpd => tpd.TrackedPeriodId == tp.Id));
            tp           = CalculateTotals(tp, days);
            tp.Name      = trackedPeriodDto.Name;
            tp.UpdatedAt = DateTime.Now;

            days.ForEach(d =>
            {
                var tpd = new TrackedPeriodDay
                {
                    Day           = d,
                    TrackedPeriod = tp
                };
                _context.Add(tpd);
            });

            _context.Entry(tp).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Tracked period updated succesfully"));
        }
Exemple #7
0
        public async Task <ResultDto> UpdateLike(LikeDto likeDto, string userId)
        {
            Like originalLike = await _context.Likes.Where(l => l.User.UUID == userId && l.SharedMeal.UUID == likeDto.SharedMealUUID)
                                .FirstOrDefaultAsync();

            if (originalLike == null)
            {
                _logger.LogDebug("Like not found with id: " + likeDto.UUID + " and with userId " + userId);
                return(new ResultDto(false, "Like not found"));
            }
            originalLike.UpdatedAt = DateTime.Now;
            originalLike.Value     = likeDto.Value;

            _context.Entry(originalLike).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Like updated succesfully"));
        }
Exemple #8
0
        public async Task <ResultDto> UpdateFeedback(FeedbackDto feedbackDto, string userId)
        {
            var originalFeedback = await _context.Feedbacks
                                   .Where(f => f.UUID == feedbackDto.UUID && f.User.UUID == userId && f.Anonymous != true)
                                   .Include(f => f.User)
                                   .FirstOrDefaultAsync();

            if (originalFeedback == null)
            {
                return(new ResultDto(false, "Feedback not found"));
            }

            originalFeedback.UpdatedAt             = DateTime.Now;
            originalFeedback.FeedbackBody          = feedbackDto.FeedbackBody;
            _context.Entry(originalFeedback).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Feedback updated succesfully"));
        }