public async Task <Response <IGame> > StartGame(StartGame game) { var response = new Response <IGame>(); try { var userOne = await _context.Users.Where(p => p.Id == game.UserId1).FirstOrDefaultAsync(); if (userOne == null) { response.Error = new Error(404, "User One not found"); return(response); } var userTwo = await _context.Users.Where(p => p.Id == game.UserId2).FirstOrDefaultAsync(); if (userTwo == null) { response.Error = new Error(404, "User Two not found"); return(response); } var newGame = new GameEntity(); await _context.Games.AddAsync(newGame); await _context.SaveChangesAsync(); var testList = await _context.Test.ToListAsync(); Random random = new Random(); int b = testList.Capacity; bool temp = true; do { int tempIndex = random.Next(0, b); var test = await _context.Test.Where(p => p.Id == tempIndex).FirstOrDefaultAsync(); if (test != null) { await _context.TestsForGame.AddAsync(new TestForGameEntity(test, newGame)); temp = false; } } while (temp); await _context.PlayerForGame.AddAsync(new PlayerForGameEntity(userOne, newGame)); await _context.PlayerForGame.AddAsync(new PlayerForGameEntity(userTwo, newGame)); await _context.SaveChangesAsync(); response.Data = newGame; } catch (Exception e) { response.Error = new Error(500, "Can`t start game: " + e); } return(response); }
public async Task <Response <string> > CreateReservationAsync(ReservationDTO dto) { var response = new Response <string>(); var dateOfReservation = new DateTime(dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, 0); var objToBD = new Reservation() { clientName = dto.clientName, clientPhone = dto.clientPhone, clientEmail = dto.clientEmail, tableNumber = dto.tableNumber, dateOfReservation = dateOfReservation, isConfirmed = false }; var result = await _db.Reservations.AddAsync(objToBD); if (result.State.ToString() == "Added") { await _db.SaveChangesAsync(); response.Data = "Reservation save succesed!"; } else { response.Error = new Error(500, "Error adding reservation!"); } return(response); }
public async Task <IActionResult> Create([Bind("Id,Title,IRNumber,IncidentDate,ReportedDate,Details,IncidentLocation,Status")] InitialReport initialReport) { if (ModelState.IsValid) { _context.Add(initialReport); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(initialReport)); }
public async Task <IActionResult> Create([Bind("Id,EmployeeNumber,FirstName,MiddleName,LastName,Alias,Position,Age,Weight,Height,Nationality,Region,CivilStatus,Birthday,IsDomainUser")] Employee employee) { if (ModelState.IsValid) { _context.Add(employee); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(employee)); }
public async Task <IActionResult> Create([Bind("Id,Name,Address")] Client client) { if (ModelState.IsValid) { _context.Add(client); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(client)); }
public async Task <ActionResult> Create([Bind(Include = "ID,機体番号,機体名,登場作品")] MS_TBL mS_TBL) { if (ModelState.IsValid) { db.MS_TBLs.Add(mS_TBL); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(mS_TBL)); }
public async Task <Response <string> > SetRatingAsync(string userName, RatingDTO ratingDTO) { var response = new Response <string>(); var ratingObjDB = await _db.Ratings .AsNoTracking() .Where(r => r.userName == userName && r.slideName == ratingDTO.slideName) .FirstOrDefaultAsync(); if (ratingObjDB == null) { var ratingObj = new Rating() { userName = userName, slideName = ratingDTO.slideName, ratingValue = ratingDTO.ratingValue }; await _db.Ratings.AddAsync(ratingObj); } else { ratingObjDB.ratingValue = ratingDTO.ratingValue; _db.Ratings.Update(ratingObjDB); } await _db.SaveChangesAsync(); response.Data = "Rating is success saved!"; return(response); }
public async Task <Response <IQuestion> > CreateQuestion(QuestionDTO createQuestion) { var response = new Response <IQuestion>(); try { if (createQuestion.Name == null) { response.Error = new Error(400, "Not valid model"); return(response); } var question = new QuestionEntity() { Name = createQuestion.Name, Description = createQuestion.Description, AnswerForQuestionEntity = new List <AnswerForQuestionEntity>() }; _context.Questions.Add(question); await _context.SaveChangesAsync(); response.Data = question; } catch (Exception e) { response.Error = new Error(500, "Can`t create question: " + e); } return(response); }
public async Task <Response <IAnswer> > CreateAnswer(CreateAnswer answer) { var response = new Response <IAnswer>(); try { var newAnswer = new AnswerEntity() { Name = answer.Name, TrueOrFalse = answer.TrueOrFalse }; _context.Answers.Add(newAnswer); await _context.SaveChangesAsync(); response.Data = newAnswer; } catch (Exception e) { response.Error = new Error(500, "Can`t register new Answer: " + e); } return(response); }
public async Task <Response <ITest> > CreateTest(string nameTest) { var response = new Response <ITest>(); try { var test = new TestEntity() { Name = nameTest }; await _context.Test.AddAsync(test); await _context.SaveChangesAsync(); response.Data = test; } catch (Exception e) { response.Error = new Error(500, "Can`t create test: " + e); } return(response); }
public async Task <MerchantDTO> Handle(DeleteMerchantCommand request, CancellationToken cancellationToken) { var data = await _context.merchants.FindAsync(request.Id); if (data == null) { return(null); } _context.merchants.Remove(data); await _context.SaveChangesAsync(); return(new MerchantDTO { Message = "Successfull", Success = true }); }
public async Task <bool> SaveAsync() { try { var changes = Db.ChangeTracker.Entries() .Count(p => p.State == EntityState.Modified || p.State == EntityState.Deleted || p.State == EntityState.Added); if (changes == 0) { return(true); } return(await Db.SaveChangesAsync() > 0); } catch (Exception) { return(false); } }
public async Task <Response <Dictionary <string, string> > > addMessage(IContactUsDTO obj) { var response = new Response <Dictionary <string, string> >(); var objToDB = new ContactUs() { clientName = obj.clientName, clientEmail = obj.clientEmail, clientSubject = obj.clientSubject, clientMessage = obj.clientMessage }; await _db.ContactUs.AddAsync(objToDB); await _db.SaveChangesAsync(); response.Data = await sendToEmail(obj); return(response); }
public async Task <GetMerchantDTO> Handle(PostMerchantCommand request, CancellationToken cancellationToken) { _context.merchants.Add(request.Data.Attributes); await _context.SaveChangesAsync(); return(new GetMerchantDTO { Message = "Success retreiving data", Success = true, Data = new MerchantData { Id = request.Data.Attributes.Id, name = request.Data.Attributes.name, image = request.Data.Attributes.image, email = request.Data.Attributes.email, address = request.Data.Attributes.address, rating = request.Data.Attributes.rating } }); }
public async Task <Response <OperationResultEnum> > CreateAccount(CreateAccount createAccount) { var response = new Response <OperationResultEnum>() { Data = OperationResultEnum.Fail }; try { if (createAccount.Nike == null) { response.Error = new Error(400, "Missed Nike"); return(response); } if (string.IsNullOrEmpty(createAccount.Password)) { response.Error = new Error(400, "Missed Password"); return(response); } if (string.IsNullOrEmpty(createAccount.ConfirmPassword)) { response.Error = new Error(400, "Missed Confirm Password"); return(response); } var isNikeAlreadyUsed = await _context.Users.Where(p => p.Nike == createAccount.Nike).FirstOrDefaultAsync(); if (isNikeAlreadyUsed != null) { response.Error = new Error(400, "This Nike is already used!"); return(response); } if (createAccount.Password != createAccount.ConfirmPassword) { response.Error = new Error(400, "Confirm password is not equals with password!"); return(response); } UserRole role = UserRole.User; var user = new UserEntity() { Id = Guid.NewGuid().ToString(), Nike = createAccount.Nike, Password = createAccount.Password, Role = role }; await _context.Users.AddAsync(user); var confirmationToken = Guid.NewGuid().ToString(); await _context.Tokens.AddAsync(new TokenEntity { UsserId = user.Id, ExpirationDateTime = DateTime.UtcNow.AddDays(1), Id = confirmationToken }); await _context.SaveChangesAsync(); response.Data = OperationResultEnum.Success; } catch (Exception ex) { response.Error = new Error(500, "Can`t register new User: " + ex); } return(response); }
public virtual async Task DeleteAsync(T entity) { entity_.Remove(entity); await context.SaveChangesAsync(); }