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")); }
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")); }
public async Task <ResultDto> AddNewDay(DayDto dayDto, string userId) { var day = _mapper.Map <Day>(dayDto); day.User = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync(); day.Meals = _mealService.AddMeals(dayDto.AllMeals, userId); day.UUID = Guid.NewGuid().ToString(); day.CreatedAt = DateTime.Now; day.UpdatedAt = DateTime.Now; day.HasVersions = false; day.IsLatest = true; _context.Add(day); await _context.SaveChangesAsync(); return(new ResultDto(true, "Day added succesfully")); }
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")); }
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")); }
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")); }
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); }