Exemplo n.º 1
0
        public async Task <IHttpActionResult> Put(InternShip data)
        {
            string methodNameStr = $"InternShipController().Put({data})";

            try
            {
                logger.Log(LogLevel.Info, $"{MakeLogStr4Entry(methodNameStr)}.");
                using (var context = new SampDB())
                {
                    var repos = new GenericDataRepository(context);
                    var res   = await repos.UpdateAsync <InternShip>(data);

                    if (res == HttpStatusCode.OK)
                    {
                        await context.SaveChangesAsync();
                    }
                    logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {res}");
                    return(Content(res, res.ToString()));
                }
            }
            catch (Exception e)
            {
                logger.Log(LogLevel.Error, $"{MakeLogStr4Exit(methodNameStr)}:\r\n{e}");
                return(Content(HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError.ToString()));
            }
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> Put([FromBody] StudentDTO StudentDTO)
        {
            string methodNameStr = $"StudentController().put()";

            logger.Log(LogLevel.Error, $"{MakeLogStr4Entry(methodNameStr)} {StudentDTO}.");

            if (!ModelState.IsValid)
            {
                logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {HttpStatusCode.BadRequest}: Modelstate is not valid.");
                return(Content(HttpStatusCode.BadRequest, "Modelstate is not valid."));
            }

            using (var context = new SampDB())
            {
                try
                {
                    List <int> CourseIds   = Get_MKeysFromDTO(StudentDTO);
                    var        StudentData = GetData(StudentDTO, new Student());
                    var        repos       = new GenericDataRepository(context);

                    var httpStatusCode = await repos.UpdateJoinEntityAsync <Student, Course>(StudentData, Convert1DTo2D(CourseIds));

                    if (httpStatusCode == HttpStatusCode.OK)
                    {
                        context.SaveChanges();
                        StudentData    = GetData(StudentDTO, StudentData);
                        httpStatusCode = await repos.UpdateAsync <Student>(StudentData);

                        if (httpStatusCode == HttpStatusCode.OK)
                        {
                            context.SaveChanges();
                        }
                    }

                    logger.Log(LogLevel.Info, $"{MakeLogStr4Exit(methodNameStr)} {httpStatusCode}.");

                    if (httpStatusCode == HttpStatusCode.OK)
                    {
                        return(Content(httpStatusCode, httpStatusCode.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()));
                }
            }
        }