Esempio n. 1
0
        public async Task <ResultDto> AddNewSharedMeal(SharedMealDto sharedMealDto, string userId)
        {
            var user = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync();

            if (user == null)
            {
                return(new ResultDto(false, "Unauthorized"));
            }

            List <Food> foods      = _foodService.MapFoodDtoListToFoodList(sharedMealDto.Foods);
            var         sharedMeal = _mapper.Map <SharedMeal>(sharedMealDto);

            sharedMeal.UUID      = Guid.NewGuid().ToString();
            sharedMeal.User      = user;
            sharedMeal.CreatedAt = DateTime.Now;
            sharedMeal.UpdatedAt = DateTime.Now;
            await _context.AddAsync(sharedMeal);

            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);
            });

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Meal shared succesfully"));
        }
Esempio n. 2
0
        public async Task <ResultDto> AddNewFood(FoodDto foodDto, string userId)
        {
            var food = _mapper.Map <Food>(foodDto);

            food.User = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync();

            food.UUID      = Guid.NewGuid().ToString();
            food.CreatedAt = DateTime.Now;
            food.UpdatedAt = DateTime.Now;
            _context.Add(food);
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Food added succesfully"));
        }
Esempio n. 3
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"));
        }
Esempio n. 4
0
        public async Task <ResultDto> AddNewFeedback(FeedbackDto feedbackDto, string userId)
        {
            var feedback = new Feedback
            {
                User         = _context.Users.Where(u => u.UUID == userId).FirstOrDefault(),
                FeedbackBody = feedbackDto.FeedbackBody,
                CreatedAt    = DateTime.Now,
                UpdatedAt    = DateTime.Now,
                UUID         = Guid.NewGuid().ToString(),
                Anonymous    = feedbackDto.UserId.Length == 0
            };

            _context.Add(feedback);
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Feedback added succesfully"));
        }
Esempio n. 5
0
        public async Task <ResultDto> AddNewLike(LikeDto likeDto, string userId)
        {
            var like = _mapper.Map <Like>(likeDto);

            like.UUID = Guid.NewGuid().ToString();
            like.User = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync();

            like.CreatedAt  = DateTime.Now;
            like.UpdatedAt  = DateTime.Now;
            like.SharedMeal = await _context.SharedMeals.Where(sm => sm.UUID == likeDto.SharedMealUUID)
                              .FirstOrDefaultAsync();

            _context.Add(like);
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Like added succesfully"));
        }
Esempio n. 6
0
        public async Task <ResultDto> AddNewTrackedPeriod(NewTrackedPeriodDto newTrackedPeriodDto, string userId)
        {
            var days = new List <Day>();

            newTrackedPeriodDto.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"));
            }

            var tp = new TrackedPeriod
            {
                UUID      = Guid.NewGuid().ToString(),
                Name      = newTrackedPeriodDto.Name,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now,
                User      = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync(),
            };

            tp = CalculateTotals(tp, days);

            _context.Add(tp);

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

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "New period saved succesfully"));
        }
Esempio n. 7
0
        public async Task <ActionResult <ResultDto> > PdfCreated(UserPdfDto userPdfDto)
        {
            var user = await _context.Users.Where(u => u.UUID == userPdfDto.User).FirstOrDefaultAsync();

            await _context.UserPDFs.AddAsync(
                new UserPDF {
                UUID      = Guid.NewGuid().ToString(),
                User      = user,
                Day       = userPdfDto.Day != null ? await _context.Days.Where(d => d.UUID == userPdfDto.Day).FirstOrDefaultAsync() : null,
                CreatedAt = DateTime.Now
            }
                );

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Stats updated"));
        }
Esempio n. 8
0
        public async Task <ActionResult <ResultDto> > RestoreDayFromVersionHistory(string dayId, string userId)
        {
            var day = await _context.Days.Where(
                d => d.UUID == dayId &&
                d.User.UUID == userId &&
                d.IsLatest == false)
                      .FirstOrDefaultAsync();

            if (day == null)
            {
                _logger.LogError($"Couldn't find day with given UUID. Not found: {dayId}");
                return(new ResultDto(false, "Day not found"));
            }

            var currentLatestDay = await _context.Days.Where(d => d.UUID == day.LatestVersionId).FirstOrDefaultAsync();

            if (currentLatestDay == null)
            {
                _logger.LogError(
                    $"Couldn't find day that has UUID which was set as LatestVersionId. Not found: {0}, Day that has it set as LatestVersionId: {1}",
                    day.LatestVersionId, day.Id);
                return(new ResultDto(false, "Something went wrong."));
            }

            day.IsLatest        = true;
            day.UpdatedAt       = DateTime.Now;
            day.VersionCreated  = null;
            day.HasVersions     = true;
            day.LatestVersionId = null;

            currentLatestDay.IsLatest        = false;
            currentLatestDay.UpdatedAt       = DateTime.Now;
            currentLatestDay.VersionCreated  = DateTime.Now;
            currentLatestDay.HasVersions     = true;
            currentLatestDay.LatestVersionId = day.UUID;

            await _context.SaveChangesAsync();

            var daysToUpdate = await _context.Days.Where(
                d => d.LatestVersionId == currentLatestDay.UUID && d.IsLatest == false)
                               .ToListAsync();

            daysToUpdate.ForEach(d => d.LatestVersionId = day.UUID);

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Day restored succesfully"));
        }
Esempio n. 9
0
        public async Task <ResultDto> RegisterUser(UserDto userDto, string password)
        {
            var user = _mapper.Map <User>(userDto);

            if (string.IsNullOrWhiteSpace(password))
            {
                return(new ResultDto(false, "Password is required"));
            }

            if (_context.Users.Any(u => u.Username == user.Username))
            {
                return(new ResultDto(false, "Username is already taken"));
            }

            if (user.Email != null && _context.Users.Any(u => u.Email == user.Email))
            {
                return(new ResultDto(false, "Email is already taken"));
            }
            try
            {
                user.Password  = HashPassword(password);
                user.MealNames = _mealService.GenerateDefaultMealNamesForUser();
                user.UUID      = Guid.NewGuid().ToString();
                user.CreatedAt = DateTime.Now;
                user.UpdatedAt = DateTime.Now;
                user.Lang      = "fi";
                _context.Add(user);

                await _context.SaveChangesAsync();

                return(new ResultDto(true, "Registration succesful"));
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
            }
            return(null);
        }