public async Task <IActionResult> ProposeBack(int id, [FromForm] BackRequestModel backRqModel) { if (backRqModel.Image != null) { if (backRqModel.Image.Length > 5242880) { ModelState.AddModelError("Image", "File is not exceeded 5MB."); } if (!ImageUtil.CheckExtensions(backRqModel.Image)) { ModelState.AddModelError("Image", "Accepted images that images are with an extension of .png, .jpg, .jpeg or .bmp."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } } // if ((backRqModel.Meaning == null || backRqModel.Meaning.Length == 0) // && (backRqModel.Image == null || backRqModel.Image.Length == 0)) // { // ModelState.AddModelError("", "Card must at least have either meaning or image."); // } // if (!ModelState.IsValid) // { // return BadRequest(ModelState); // } var userId = UserUtil.GetUserId(User); var admin = await userManager.GetAdmin(); var card = await repository.Card .QueryById(admin.Id, id) .FirstOrDefaultAsync(); if (card == null) { return(NotFound()); } var imageName = await imageService.UploadImage(backRqModel.Image, ImageType.Image); var now = DateTime.Now; var newBack = new Back() { Type = backRqModel.Type == null || backRqModel.Type.Trim().Length == 0 ? null : backRqModel.Type.Trim().ToLower(), Meaning = backRqModel.Meaning == null || backRqModel.Meaning.Trim().Length == 0 ? null : backRqModel.Meaning.Trim(), Example = backRqModel.Example == null || backRqModel.Example.Trim().Length == 0 ? null : backRqModel.Example.Trim(), Image = imageName, Public = true, Approved = false, CreatedDate = now, LastModifiedDate = now, Card = card, AuthorId = userId }; repository.Back.Create(newBack); await repository.SaveChangesAsync(); return(CreatedAtAction("GetProposedBackById", "ProposedBacks", new { Id = newBack.Id }, new { Message = "Created Successfully.", Id = newBack.Id })); }
public async Task <IActionResult> CreateBack(int id, [FromForm] BackRequestModel backRqModel) { if (backRqModel.Image != null) { if (backRqModel.Image.Length > 5242880) { ModelState.AddModelError("Image", "File is not exceeded 5MB."); } if (!ImageUtil.CheckExtensions(backRqModel.Image)) { ModelState.AddModelError("Image", "Accepted images that images are with an extension of .png, .jpg, .jpeg or .bmp."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } } var user = await UserUtil.GetUser(userManager, User); var card = await repository.Card .QueryById(user.Id, id) .FirstOrDefaultAsync(); if (card == null) { return(NotFound()); } var imageName = await imageService.UploadImage(backRqModel.Image, ImageType.Image); var userIsAdmin = await userManager.IsInRoleAsync(user, Roles.Administrator); var now = DateTime.Now; var newBack = new Back() { Type = backRqModel.Type == null || backRqModel.Type.Trim().Length == 0 ? null : backRqModel.Type.Trim().ToLower(), Meaning = backRqModel.Meaning == null || backRqModel.Meaning.Trim().Length == 0 ? null : backRqModel.Meaning.Trim(), Example = backRqModel.Example == null || backRqModel.Example.Trim().Length == 0 ? null : backRqModel.Example.Trim(), Image = imageName, Public = userIsAdmin, Approved = userIsAdmin, CreatedDate = now, LastModifiedDate = now, Card = card, Author = user }; repository.Back.Create(newBack); var decks = await repository.Deck .QueryByCardId(card.Id) .ToListAsync(); foreach (var deck in decks) { deck.Approved = deck.Approved && userIsAdmin; } await repository.SaveChangesAsync(); return(CreatedAtAction("GetBack", "Backs", new { Id = newBack.Id }, new { Message = "Created Successfully.", Id = newBack.Id })); }