Example #1
0
        // PUT api/Groups
        public HttpResponseMessage PutGroup(Group group)
        {
          if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
          using (var context = new StatisticContext())
          {
            var addedClients = group.Clients.Select(x => x.Id).ToList();
            group.Clients.Clear();

            foreach (var @client in
              from @client in context.Client
              from added in addedClients.Where(added => @client.Id == added)
              select @client)
            {
              group.Clients.Add(@client);
            }

            try
            {
              context.Group.Add(group);
              context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
              return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
          }

          HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, group);
          response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = group.Id }));
          return response;
        }
Example #2
0
        // POST api/Groups/5
        public HttpResponseMessage PostGroup(Group group)
        {
          if (!ModelState.IsValid)
          {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
          }

          if (group == null || group.Id <= 0)
          {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
          }

          var addedClients = group.Clients.Select(x => x.Id).ToList();
          group.Clients.Clear();

          var id = group.Id;
          using (var context = new StatisticContext())
          {
            var dbGroup = context.Group.Include(x => x.Clients).FirstOrDefault(x => x.Id == id && x.Clients.Any());
            if (dbGroup != null)
            {
              dbGroup.Clients.Clear();
              try
              {
                context.SaveChanges();
              }
              catch (DbUpdateConcurrencyException ex)
              {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
              }
            }
          }

          using (var context = new StatisticContext())
          {
            var dbGroup = context.Group.Include(x => x.Clients).FirstOrDefault(x => x.Id == group.Id);
            if (dbGroup == null)
            {
              return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            foreach (var @client in
              from @client in context.Client
              from added in addedClients.Where(added => @client.Id == added)
              select @client)
            {
              dbGroup.Clients.Add(@client);
            }

            dbGroup.Title = group.Title;
            dbGroup.Color = group.Color;

            try
            {
              context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
              return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
          }

          return Request.CreateResponse(HttpStatusCode.OK);
        }