public async Task <IHttpActionResult> Post(InternShip InternShipData) { string methodNameStr = $"InternShipController().Post({InternShipData})"; try { logger.Log(LogLevel.Info, $"{MakeLogStr4Entry(methodNameStr)}."); using (var context = new SampDB()) { var repos = new GenericDataRepository(context); var httpStatusCode = await repos.InsertAsync <InternShip>(InternShipData); if (httpStatusCode == HttpStatusCode.OK) { await context.SaveChangesAsync(); } logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {httpStatusCode}"); if (httpStatusCode == HttpStatusCode.OK) { return(Content(httpStatusCode, InternShipData.Id.ToString())); } else { return(Content(httpStatusCode, httpStatusCode.ToString())); } } } catch (Exception e) { logger.Log(LogLevel.Error, $"{MakeLogStr4Exit(methodNameStr)}:\r\n{e}"); return(Content(HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError.ToString())); } }
public async Task <IHttpActionResult> Post([FromBody] StudentDTO StudentDTO) { string methodNameStr = $"StudentController().post()"; logger.Log(LogLevel.Info, $"{MakeLogStr4Entry(methodNameStr)} {StudentDTO}."); if (!ModelState.IsValid) { logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {HttpStatusCode.BadRequest}: Modelstate is not valid."); return(Content(HttpStatusCode.BadRequest, "The validation of the Data Transfer Object fails.")); } using (var context = new SampDB()) { using (var transaction = context.Database.BeginTransaction()) { try { List <int> CourseIds = Get_MKeysFromDTO(StudentDTO); var StudentData = GetData(StudentDTO, new Student()); var repos = new GenericDataRepository(context); var httpStatusCode = await repos.InsertAsync <Student>(StudentData); if (httpStatusCode == HttpStatusCode.OK) { context.SaveChanges(); httpStatusCode = await repos.UpdateJoinEntityAsync <Student, Course>(StudentData, Convert1DTo2D(CourseIds)); if (httpStatusCode == HttpStatusCode.OK) { context.SaveChanges(); transaction.Commit(); } else { transaction.Rollback(); } } logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {httpStatusCode}."); if (httpStatusCode == HttpStatusCode.OK) { return(Content(httpStatusCode, StudentData.Id.ToString())); } else { return(Content(httpStatusCode, httpStatusCode.ToString())); } } catch (Exception e) { transaction.Rollback(); logger.Log(LogLevel.Error, $"{MakeLogStr4Exit(methodNameStr)}:\r\n{e}"); return(Content(HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError.ToString())); } } } }