예제 #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;
        }
예제 #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);
        }
예제 #3
0
        // DELETE api/Groups/5
        public HttpResponseMessage DeleteGroup(int id)
        {
          using (var context = new StatisticContext())
          {
            Group group = context.Group.Find(id);
            if (group == null)
            {
              return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            context.Group.Remove(group);

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

            return Request.CreateResponse(HttpStatusCode.OK, group);
          }
        }
예제 #4
0
    // POST api/Clients/5
    public HttpResponseMessage PostClient(Client client)
    {
      if (!ModelState.IsValid)
      {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
      }

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

      var addedGroups = client.Groups.Select(x => x.Id).ToList();
      client.Groups.Clear();

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

      using (var context = new StatisticContext())
      {
        var dbClient = context.Client.Include(x => x.Groups).FirstOrDefault(x => x.Id == client.Id);
        if (dbClient == null)
        {
          return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        foreach (var @group in
          from @group in context.Group
          from added in addedGroups.Where(added => @group.Id == added)
          select @group)
        {
          dbClient.Groups.Add(@group);
        }

        dbClient.FirstName = client.FirstName;
        dbClient.LastName = client.LastName;
        dbClient.Email = client.Email;
        dbClient.BirthDate = client.BirthDate;
        dbClient.City = client.City;
        dbClient.Gender = client.Gender;

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

      return Request.CreateResponse(HttpStatusCode.OK);
    }