public async Task <IActionResult> CreateReport([FromBody] ReportForCreationDto report) { if (report == null) { _logger.LogError("Report object sent from client is null."); return(BadRequest("Report object is null")); } //This checks all of the validation attributes on the object to make sure the values are valid if (!ModelState.IsValid) { _logger.LogError("Invalid report object sent from client."); return(BadRequest("Invalid model object")); } var reportEntity = _mapper.Map <Report>(report); reportEntity.DateCreated = DateTime.Now; _repoWrapper.Report.CreateReport(reportEntity); await _repoWrapper.SaveAsync(); var createdReport = _mapper.Map <ReportDto>(reportEntity); return(CreatedAtRoute("ReportById", new { id = createdReport.ReportId }, createdReport)); }
public async Task <ActionResult> ReportUser([FromBody] ReportForCreationDto reportForCreationDto) { var userReporter = await _userRepo.GetUserByUserClaims(HttpContext.User); if (userReporter == null) { return(Unauthorized("User is Unauthorized")); } var userReported = await _userRepo.GetUserByUsername(reportForCreationDto.Username); if (userReported == null) { return(BadRequest("User Not Found")); } if (userReporter.Id == userReported.Id) { return(BadRequest("You Can't Report Yourself")); } var lastReportDate = await _reportRepo.GetLastReportDate(userReporter.Id, userReported.Id); if (DateTime.Now < lastReportDate.AddDays(1)) { return(BadRequest("User Can't Report Same User more than once in a day")); } var newReport = new Report { UserSourceReportId = userReporter.Id, UserDestinationReportId = userReported.Id, ReportString = reportForCreationDto.ReportString, ReportDate = DateTime.Now }; var result = await _reportRepo.AddReport(newReport); if (result) { return(Ok("Report done successfully")); } throw new Exception("Error Occured While Reporting User"); }