public async Task <IActionResult> CreateUserMealProfile([FromBody] UserMealProfilingSaveModel model) { if (!ModelState.IsValid) { return(BadRequest()); } UserMealProfiling newProfile = await context.UserMealProfilings.FirstOrDefaultAsync(u => u.UserId == model.UserId); if (newProfile != null) { return(StatusCode(400, "Profiling already exist")); } var entity = mapper.Map <UserMealProfilingSaveModel, UserMealProfiling>(model); context.UserMealProfilings.Add(entity); await context.SaveChangesAsync(); entity = await context.UserMealProfilings .Include(u => u.User) .Include(d => d.DepartmentMealProfiling) .SingleOrDefaultAsync(it => it.Id == entity.Id); var result = mapper.Map <UserMealProfiling, UserMealProfilingModel>(entity); return(Ok(result)); }
public async Task <IActionResult> CreateMealTransaction([FromBody] MealTransactionSaveModel model) { if (!ModelState.IsValid) { return(BadRequest()); } //Todo: get the user mealprofile and check for today validity UserMealProfiling newProfile = await context.UserMealProfilings .Include(d => d.DepartmentMealProfiling).ThenInclude(m => m.MealAssignment) .FirstOrDefaultAsync(u => u.UserId == model.UserId); // check if user have exhausted the collection for the day var startDateStr = ""; var endDateStr = ""; DateTime startDate = DateTime.Now; DateTime endDate = DateTime.Now; System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"); startDateStr = Convert.ToDateTime(startDate, ci.DateTimeFormat).ToString("d"); //short date pattern endDateStr = Convert.ToDateTime(endDate, ci.DateTimeFormat).ToString("d"); //short date pattern var startTime = TimeSpan.Parse("00:00:00"); var endTime = TimeSpan.Parse("23:59:59"); DateTime todayStartDate = Convert.ToDateTime(startDateStr) + startTime; DateTime todayEndDate = Convert.ToDateTime(endDateStr) + endTime; var query = context.MealTransactions.Where(p => p.UserId == model.UserId && (p.CreatedOn >= todayStartDate && p.CreatedOn <= todayEndDate)); if (query.Count() > newProfile.DepartmentMealProfiling.MealAssignment.MealEntitled) { StatusCode(400, "Meal Entitlment Exceeded"); } var entity = mapper.Map <MealTransactionSaveModel, MealTransaction>(model); entity.UserMealProfilingId = newProfile.Id; entity.CreatedOn = DateTime.Now; context.MealTransactions.Add(entity); await context.SaveChangesAsync(); entity = await context.MealTransactions .Include(m => m.Menu) .Include(d => d.UserMealProfiling) .Include(u => u.User) .SingleOrDefaultAsync(it => it.Id == entity.Id); var result = mapper.Map <MealTransaction, MealTransactionModel>(entity); return(Ok(result)); }
public async Task <IActionResult> CreateClientUser([FromBody] AccountSaveResource model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = model.Username, Email = model.Email, EmailConfirmed = true, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, IsEnabled = true }; model.Password = "******"; IdentityResult addUserResult = await userManager.CreateAsync(user, model.Password); if (!addUserResult.Succeeded) { return(StatusCode(400, addUserResult)); } DepartmentMealProfiling departProfile = await context.DepartmentMealProfilings.Where(d => d.DepartmentId == model.DepartmentId).FirstOrDefaultAsync(); var userProfiling = new UserMealProfiling() { UserId = user.Id, DepartmentMealProfilingId = departProfile.Id }; context.UserMealProfilings.Add(userProfiling); await context.SaveChangesAsync(); var result = mapper.Map <ApplicationUser, ApplicationUserModel>(user); return(StatusCode(200, result)); }