public async Task <Response> Rate(CreateThreadRatingDto createThreadRatingDto) { var thread = await _context.Threads. SingleOrDefaultAsync(t => t.ThreadId == createThreadRatingDto.ThreadId); //var existingThreadRating = await _context.ThreadRatings // .Include(tr => tr.Thread) // .Include(tr => tr.User) // .SingleOrDefaultAsync(tr => // tr.Thread.ThreadId == thread.ThreadId && // tr.User.Id == _userService.GetUserId()); if (!Enum.IsDefined(typeof(Rating), createThreadRatingDto.Type)) { return(new Response().WithError("*", "Invalid Rating Type")); } //if (existingThreadRating != null) //{ // //user already rated this thread, either change the rating type or do nothing // if (existingThreadRating.Type != createThreadRatingDto.Type) // { // existingThreadRating.Type = createThreadRatingDto.Type; // await _context.SaveChangesAsync(); // return new Response().WithSuccess(); // } // return new Response() // .WithError("*", "Rating already exists"); //} var threadRatingEntity = _mapper.Map <ThreadRating>(createThreadRatingDto); threadRatingEntity.CreatedOn = DateTime.UtcNow; //threadRatingEntity.User = await _userService.GetUser(); thread.ThreadRatings.Add(threadRatingEntity); await _context.SaveChangesAsync(); return(new Response().WithSuccess()); }
//[Authorize] public async Task <IActionResult> Rate(CreateThreadRatingDto createThreadRatingDto) { return(this.GenerateResponse(await _threadService.Rate(createThreadRatingDto))); }